
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>简易计算器</title>
</head>
<body>
<p>简易计算器</p>
第一个数:<input id="num1" type="text" name="" id="" value="" /><br>
第二个数:<input id="num2" type="text" name="" id="" value="" /><br>
<button type="button" value="+" onclick="cal('+')">+</button>
<button type="button" value="-" onclick="cal('-')">-</button>
<button type="button" value="*" onclick="cal('*')">*</button>
<button type="button" value="/" onclick="cal('/')">/</button><br>
计算结果:<input type="text" name="" id="resultset" value="" />
<script type="text/javascript">
function cal(type){
var num1 = document.getElementById("num1");
var num2 = document.getElementById("num2");
switch(type){
case "+":
var result = parseInt(num1.value) + parseInt(num2.value);
break;
case "-":
var result = parseInt(num1.value) - parseInt(num2.value);
break;
case "*":
var result = parseInt(num1.value) * parseInt(num2.value);
break;
case "/":
var result = parseInt(num1.value) / parseInt(num2.value);
break;
}
console.log(result);
resultset.value = result;
}
</script>
</body>
</html>

615

被折叠的 条评论
为什么被折叠?



