清除功能没有做任何事情

时间:2019-06-14 14:56:43

标签: javascript html css function calculator

我通过Javascript制作了一个基本的计算器,所有其他功能(例如加,减等)都可以使用,但是clear函数没有任何作用,开发人员工具中也没有出现任何错误。 / p>

我尝试过保留清除功能,甚至删除注释,但没有任何改变。

//function to clear the numbers together
function clearNumbers() {

    // make variable for the value of box1
    var value1 = "";

    // make a variable for the value of box2
    var value2 = "";

    // make a variable called total
    var total = "";

    // put total in the output span
    document.getElementById("output").innerHTML = "";

    // put a   sign in the sign span
    document.getElementById("sign").innerHTML = " ";
}

<input type="button" value="Clear" onClick="clearNumbers();">

没有错误消息

1 个答案:

答案 0 :(得分:1)

您需要设置document.getElementById("box1").value = "";来清除文本框值。

//function to divide the numbers together
function divideNumbers() {

// make variable for the value of box1
var value1 = parseFloat(document.getElementById("box1").value);

// make a variable for the value of box2
var value2 = parseFloat(document.getElementById("box2").value);

// make a variable called total
var total = value1 / value2;

// put total in the output span
document.getElementById("output").innerHTML = total;

// put a &divide; sign in the sign span
document.getElementById("sign").innerHTML = "&divide;";}



//function to clear the numbers together
function clearNumbers() {

// make variable for the value of box1
var value1 = "";
document.getElementById("box1").value = "";
// make a variable for the value of box2
var value2 = "";
document.getElementById("box2").value = "";
// make a variable called total
var total = "";

// put total in the output span
document.getElementById("output").innerHTML = "";

// put a &nbsp; sign in the sign span
document.getElementById("sign").innerHTML = "&nbsp;";}
<input type='text' id='box1' />
<div id='sign'></div>
<input type='text' id='box2' />
<input type='text' id='output' />
<input type="button" value="Clear" onClick="clearNumbers();">