<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>两数之间的运算</title>
<script type="text/javascript">
function toResult()
{
var n1=parseInt(document.getElementById("txtN1").value);
var n2=parseInt(document.getElementById("txtN2").value);
var c=document.getElementById("caul").value;
var r;
switch(c)
{
case "+": r= n1+n2; break;
case "-": r= n1-n2; break;
case "*": r= n1*n2; break;
case "/": r= n1/n2; break;
case "%": r= n1% n2; break
}
document.getElementById("rs").innerHTML=r.toString();
}
}
</script>
</head>
<body>
<input type="text" name="txtN1" id="txtN1"/>
<select name="caul" id="caul" >
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
<option>%</option>
</select>
<input type="text" name="txtN2" id="txtN2"/>
<input type="button" name="btnR" value="=" onclick="toResult()"/ >
<span id="rs">?</span>
</body>
</html>
1 parseInr()用错地方了,把n1 n2里面的parseInt()去掉 ,2在case +-×/% 后 r=r= n1+n2 改成 r= parseInt(n1)+parseInt(n2); ,3function toresult( ){ } 后面多了一个 }
还真可以!我看到很多出错的人都是parseInt()加在var n1 n2 后面,还以为是错误语法呢!我用的是WS,用chrome浏览器纠错比较强。