[英]Simple Calaculator in HTML and Javascript does not work
我有这个HTML(只是一个简单的袖珍计算器):
<html>
<head>
<script type="text/javascript" src="js/SimpleCalculator.js"></script>
</head>
<body>
<h2>Simple Calculator</h2>
<div class="calculator">
<div class="display"><input type="text" readonly size="20" id="dsp"></div>
<div class="keys">
<p><input type="button" value="7" onclick='getValue(this.value)'>
<input type="button" value="8" onclick='getValue(this.value)'>
<input type="button" value="9" onclick='getValue(this.value)'>
<input type="button" value="/" onclick='getValue(this.value)'>
</p>
<p><input type="button" value="4" onclick='getValue(this.value)'>
<input type="button" value="5" onclick='getValue(this.value)'>
<input type="button" value="6" onclick='getValue(this.value)'>
<input type="button" value="*" onclick='getValue(this.value)'>
</p>
<p><input type="button" value="1" onclick='getValue(this.value)'>
<input type="button" value="2" onclick='getValue(this.value)'>
<input type="button" value="3" onclick='getValue(this.value)'>
<input type="button" value="-" onclick='getValue(this.value)'>
</p>
<p><input type="button" value="0" onclick='getValue(this.value)'>
<input type="button" value="." onclick='getValue(this.value)'>
<input type="button" value="+" onclick='getValue(this.value)'></p>
<p><input type="button" value="C" onclick='getValue(this.value)'>
<input type="button" value="=" onclick='getValue(this.value)'></p>
</div>
</div>
</body>
</html>
和JS文件:
var s = '';
function getValue(val){
alert(val);
if (val != 'C' && val != '='){s += val;
document.getElementById("dsp").value+=val;}
if (val === 'C')
document.getElementById("dsp").value = " ";
if (val === '='){
alert(document.getElementByVal("dsp").value);
var res = eval(s);
document.getElementByVal("dsp").value = res;
}
}
将显示键(数字和数学符号),警报可以正常工作,但不显示支出评估。 我哪里错了?
您应该使用getElementById
而不是ByVal
,这不是一个函数。
var s = ''; function getValue(val){ if (val != 'C' && val != '='){s += val; document.getElementById("dsp").value+=val;} if (val === 'C') document.getElementById("dsp").value = " "; if (val === '='){ alert(document.getElementById("dsp").value); var res = eval(s); document.getElementById("dsp").value = res; } }
<h2>Simple Calculator</h2> <div class="calculator"> <div class="display"><input type="text" readonly size="20" id="dsp"></div> <div class="keys"> <p><input type="button" value="7" onclick='getValue(this.value)'> <input type="button" value="8" onclick='getValue(this.value)'> <input type="button" value="9" onclick='getValue(this.value)'> <input type="button" value="/" onclick='getValue(this.value)'> </p> <p><input type="button" value="4" onclick='getValue(this.value)'> <input type="button" value="5" onclick='getValue(this.value)'> <input type="button" value="6" onclick='getValue(this.value)'> <input type="button" value="*" onclick='getValue(this.value)'> </p> <p><input type="button" value="1" onclick='getValue(this.value)'> <input type="button" value="2" onclick='getValue(this.value)'> <input type="button" value="3" onclick='getValue(this.value)'> <input type="button" value="-" onclick='getValue(this.value)'> </p> <p><input type="button" value="0" onclick='getValue(this.value)'> <input type="button" value="." onclick='getValue(this.value)'> <input type="button" value="+" onclick='getValue(this.value)'></p> <p><input type="button" value="C" onclick='getValue(this.value)'> <input type="button" value="=" onclick='getValue(this.value)'></p> </div> </div>
更改最后一行以使用getElementById,而不是getElementByVal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.