看到很多都是用的switch,我特意写了个用if的方式
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
var num1 = parseInt(document.getElementById("txt1").value);
var num2 = parseInt(document.getElementById("txt2").value);
var character = document.getElementById("select").value;
var result;
if(character=="+"){
result = num1+num2;
}
else if(character=="-"){
result = num1-num2;
}
else if(character=="*"){
result = num1*num2;
}
else if(character=="/"){
result = num1/num2;
}
else{
result = null;
}
document.getElementById("fruit").value = result;
}
</script>
</head>
<body>
<input type = "text" id = "txt1"/>
<select id = "select">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type = "text" id = "txt2"/>
<input type = "button" value = "=" onClick="count()"/>
<input type = "text" id = "fruit"/>
</body>
</html>