页面效果
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<title>计算器</title>
</head>
<script>
function calc(num){
//通过input_name接收输入框内容,并转化为Int类型
var n1=parseInt(window.document.getElementsByName("firNum")[0].value);
var n2=parseInt(window.document.getElementsByName("secNum")[0].value);
var result;
//通过传入参数选择加减乘除功能
switch(num){
case 1:result= n1+n2;break;
case 2:result= n1-n2;break;
case 3:result= n1*n2;break;
case 4:result= n1/n2;break;
}
//将计算结果赋给--计算结果:<input type="text" name="result">
window.document.getElementsByName("res")[0].value=result;
}
</script>
<body>
第一个数:<input type="text" name="firNum"><br>
第二个数:<input type="text" name="secNum"><br>
<button οnclick="calc(1)">+</button>
<button οnclick="calc(2)">-</button>
<button οnclick="calc(3)">*</button>
<button οnclick="calc(4)">/</button>
<br>
计算结果:<input type="text" name="res">
</body>
</html>