网页计算器
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>网页计算器</title>
</head>
<body>
<p>整数1:<input type="text" id="num1"></p>
<p>整数2:<input type="text" id="num2"></p>
<input type="button" value="相加" onclick="calc(add)" />
<input type="button" value="相减" onclick="calc(sub)" />
<input type="button" value="相乘" onclick="calc(mul)" />
<input type="button" value="相除" onclick="calc(div)" />
<p>结果:<input type="text" id="result" readonly=""></p>
<script>
function calc(func){
var result=document.getElementById('result');
var num1 = parseInt(document.getElementById('num1').value);
var num2 = parseInt(document.getElementById('num2').value);
if (isNaN(num1)||isNaN(num2)) {
alert('请输入数字');
return false;
}
result.value=func(num1,num2);
}
function add(num1,num2){
return num1 num2;
}
function sub(num1,num2){
return num1-num2;
}
function mul(num1,num2){
return num1*num2;
}
function div(num1,num2){
if(num2 == 0){
alert('除数不能为0');
return '';
}
return num1/num2;
}
</script>
</body>
</html>
评论