[英]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.