慕粉3805859
2019-01-22 23:57
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
input { width: 60px; }
div { margin: 10px 0 }
</style>
<script type="text/javascript">
window.onload = function () {
// 获取元素
var num1 = document.getElementById('num1');
var num2 = document.getElementById('num2');
var ret = document.getElementById('ret');
var btns = document.getElementsByTagName('button');
var p = parseInt;
var res=0;
//为按钮添加点击事件,计算结果
for(var i =0;i<btns.length;i++){
btns[i].onclick=function(){
var v1 = parseInt(num1.value);
var v2 = parseInt(num2.value);
// alert(this.textContent);
switch(this.textContent){
case "+": res=v1+v2;break;
case "-": res=v1-v2 ;break;
case "*": res=v1*v2 ;break;
case "/": res=v1/v2 ;break;
}
// alert(parseInt(num1.value));
console.log(res.toFixed(3))
ret.innerHTML=parseFloat(res.toFixed(3));
}
}
}
</script>
</head>
<body>
<input id="num1" type="text"/>
<input id="num2" type="text"/>
<div>结果:<span id="ret"></span></div>
<button>+</button>
<button>-</button>
<button>*</button>
<button>/</button>
</body>
</html>
都parseInt了,还parseFloat、toFiexed()?
用JS实现购物车特效
43235 学习 · 167 问题
相似问题