我正在制作一个简单的计算器而且我的功能不起作用:
HTML:
<div id="mathLine">0</div>
<div id="score">none</div>
<button id="clear" class = "num" ocnlick="clear(0)">C</button>
<button id="exp" class = "num" >exp</button>
<button id="sqrt" class = "num">sqrt</button>
<button id="buttonide" class = "num">/</button>
<button id="seven7" class = "num" onclick="numerical(7)">7</button>
<button id="eight8" class = "num" onclick="numerical(8)">8</button>
<button id="nine9" class = "num" onclick="numerical(9)">9</button>
<button id="multiply" class = "num" onclick="numerical('X')">X</button>
JS
var equation = [];
function numerical(number){
equation.push(number);
document.getElementById('mathLine').innerHTML = equation;
console.log(equation);
};
function clear (j) {
for (j; j < (equation.length+2); j++){
equation.pop();
}
console.log(equation);
document.getElementById('mathLine').innerHTML = "0";
};
更多信息:
所以我不知道,出了什么问题......也许我看着它太久了,我没有看到明显的东西?
答案 0 :(得分:1)
你有一个错字。尝试改变&#34; ocnlick&#34;到&#34; onclick。&#34;
答案 1 :(得分:0)
我的信念是你的数组清除工作不正常 - 当你从数组中删除元素时,迭代数组通常是不安全的,因为索引可能搞砸了。您可以使用while循环替换for循环来检查数组的长度,但是一个简单的建议是重新分配数组:
equation = [];