Javascript计算器简单清晰的功能不起作用

Kin*_*123 0 html javascript calculator innerhtml

我正在尝试使用Javascript构建一个简单的计算器,并且我在清除显示内容时遇到问题.

有人可以看看我的代码,让我知道它为什么不起作用.

为什么不将显示值设置为空字符串.我究竟做错了什么?坦克你们.

function testing(button){
  var x = button.value;
  document.getElementById("display").innerHTML+=x;
}

function clear() {
  document.getElementById("display").innerHTML = "";
 }
Run Code Online (Sandbox Code Playgroud)
<body>
    <input type="button" id="one" value="1" onClick="testing(this)">
    <input type="button" id="one" value="2" onClick="testing(this)">
    <input type="button" id="one" value="3" onClick="testing(this)">
    <input type="button" id="clear" value="clear" onClick="clear()">

    <h1 id="display"></h1>
  </body>
Run Code Online (Sandbox Code Playgroud)

gur*_*372 6

您的方法名称与id值冲突,只需将其更改为clear1,它应该有效.

       function testing(button){
        var x = button.value;
        document.getElementById("display").innerHTML+=x;
        }

       function clear1(){
        document.getElementById("display").innerHTML = "";
       }
Run Code Online (Sandbox Code Playgroud)
<body>
    <input type="button" id="one" value="1" onClick="testing(this)">
    <input type="button" id="one" value="2" onClick="testing(this)">
    <input type="button" id="one" value="3" onClick="testing(this)">
    <input type="button" id="clear" value="clear" onClick="clear1()">

    <h1 id="display"></h1>
    </body>
Run Code Online (Sandbox Code Playgroud)