<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<input type="text">
<select>
<option value="1">+</option>
<option value="2">-</option>
<option value="3">*</option>
<option value="4">/</option>
</select>
<input type="text"> <button type="button">=</button> <input type="text">
</body>
</html>
<script>
var aInput=document.getElementsByTagName('input');
var oBtn=document.getElementsByTagName('button')[0];
var sel=document.getElementsByTagName('select')[0];
oBtn.οnclick=function(){
var a=parseInt(aInput[0].value);
var b=parseInt(aInput[1].value);
//计算之前先判断啊a,b是否为数字
if(isNaN(a) ||isNaN(b)){
alert("请输入数字");
aInput[0].value=aInput[1].value="";
}else{
var sum=aInput[2];
switch(sel.value-0){ //将sel.value隐性转换为number类型
case 1:
sum.value=a+b;
break;
case 2:
sum.value=a-b;
break;
case 3:
sum.value=a*b;
break;
case 4:
sum.value=a/b;
break;
};
};
};
</script>