慕容6447874
2018-07-15 11:45
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function count(){
var num1=document.getElementById("num1").value ,
symble=document.getElementById("symble"), num2=document.getElementById("num2").value ,
result;
switch(symble){
case"+":
result=parseInt(num1)+parseInt(num2);
break;
case"-":
result=parseInt(num1)-parseInt(num2);
break;
case"*":
result=parseInt(num1)*parseInt(num2);
break;
case"/":
result=parseInt(num1)/parseInt(num2);
break;
default:
result=null;
}
document.getElementById("num3") = result;
}
</script>>
<title>计算器简单功能实现</title>
</head>
<body>
<input type="text" name="num" id="num1">
<select id="symble">
<option>+</option>
<option>-</option>
<option selected="selected">*</option>
<option>/</option>
</select>
<input type="text" name="num" id="num2">
<input type="button" value="=" onclick = "count()">
<input type="text" name="num" id="num3">
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function count() {
var num1 = document.getElementById("num1").value,
symble = document.getElementById("symble").value,
num2 = document.getElementById("num2").value,
result;
switch (symble) {
case "+":
result = parseInt(num1) + parseInt(num2);
break;
case "-":
result = parseInt(num1) - parseInt(num2);
break;
case "*":
result = parseInt(num1) * parseInt(num2);
break;
case "/":
result = parseInt(num1) / parseInt(num2);
break;
default:
result = null;
}
document.getElementById("num3").value = result;
}
</script>
<title>计算器简单功能实现</title>
</head>
<body>
<input type="text" name="num" id="num1">
<select id="symble">
<option>+</option>
<option>-</option>
<option selected="selected">*</option>
<option>/</option>
</select>
<input type="text" name="num" id="num2">
<input type="button" value="=" onclick="count()">
<input type="text" name="num" id="num3">
</body>
</html>
symble=document.getElementById("symble")是获取id为symble这个对象,而不是它的值。
应该写为 symble=document.getElementById("symble").value
同样的问题:
document.getElementById("num3").value = result;
JavaScript进阶篇
468194 学习 · 21891 问题
相似问题