3
<body>
<FORM NAME="Calculator">
<TABLE BORDER=4>
<TR>
<TD>
<input type="text"   name="Input" Size="22" value="">


<br>
</TD>
</TR>
<TR>
<TD>
<INPUT TYPE="button" NAME="one"   VALUE="1" class ="digit" >
<INPUT TYPE="button" NAME="two"   VALUE="2" class ="digit" >
<INPUT TYPE="button" NAME="three" VALUE="3" class ="digit" >
<INPUT TYPE="button" NAME="plus"  VALUE="+" class ="operand">
<br>
<INPUT TYPE="button" NAME="four"  VALUE="4" class ="digit">
<INPUT TYPE="button" NAME="five"  VALUE="5" class ="digit">
<INPUT TYPE="button" NAME="six"   VALUE="6" class ="digit">
<INPUT TYPE="button" NAME="minus" VALUE="-" class="operand">
<br>
<INPUT TYPE="button" NAME="seven" VALUE="7" class ="digit">
<INPUT TYPE="button" NAME="eight" VALUE="8" class ="digit">
<INPUT TYPE="button" NAME="nine"  VALUE="9" class ="digit">
<INPUT TYPE="button" NAME="times" VALUE="*" class ="operand">
<br>
<INPUT TYPE="button" NAME="clear" VALUE="c" class ="special">
<INPUT TYPE="button" NAME="zero"  VALUE="0" class ="digit">
<INPUT TYPE="button" NAME="Execute"  VALUE="=" class ="solve">
<INPUT TYPE="button" NAME="div"   VALUE="/" class ="operand">
<br>
</TD>
</TR>
</TABLE>
</FORM>

<script type = "text/javascript" src="C:\Users\Quonn\Desktop\QBJS\calculatorjs.js">
</script>
</body>

我正在构建一个可配置的计算器,但我的逻辑中有一些/让它完全按照我想要的方式运行。我有两个问题。

问题#1:我怎样才能改变我的逻辑,以便我可以替换“邪恶的评估”?

var timer;
document.onclick = function(x) {
var info = x.target;
clearTimeout(timer);
 timer= setTimeout(function(){addDigit(x);},200);
}

问题 #2:如何更改我在此函数中的逻辑,以便在显示计算结果后,为下一次计算输入的第一个数字不只是连接到前一次计算的结果?

function addDigit(x){
if (x.target.className === "digit" || x.target.className ==="operand") {
    document.Calculator.Input.value += "" + x.target.value;
}

else if (x.target.className === "solve") {
    result = eval(document.Calculator.Input.value);
    document.Calculator.Input.value = result;
}
else  {
   document.Calculator.Input.value = "";
}

}
4

1 回答 1

1

您正在连接字符串。你应该看看parseInt / parseFloat;注意基数,否则 JavaScript 会尝试猜测它...

于 2013-03-08T08:07:19.620 回答