2017-10-13 17 views
0

私は表示コンテンツを消去するときに問題があるJavascriptとImで簡単な電卓を構築しようとしています。JavaScriptの計算機のシンプルな明確な機能が動作しない

誰かが自分のコードを見て、なぜそれが動作していないのか教えてください。

なぜ表示値を空の文字列に設定しないのでしょうか。私は間違って何をしていますか?あなたたちにタンクしてください。

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

 
function clear() { 
 
    document.getElementById("display").innerHTML = ""; 
 
}
<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>

答えて

6

あなたのメソッド名がconflicting with the id valueで、ちょうどclear1に変更し、それが動作するはずです。

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

 
     function clear1(){ 
 
     document.getElementById("display").innerHTML = ""; 
 
     }
<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>

+0

感謝。 – Kingsfull123

1

問題があなたの元のコールに優先を取っているdocument.clear機能があることです。これはコンソールにdocument.clearと入力することでテストできます。

clearDisplay関数の名前を変更してください。先端のための

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

 
function clearDisplay(){ 
 
    document.getElementById("display").innerHTML = ""; 
 
}
<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="clearDisplay" value="clear" onClick="clearDisplay()"> 
 

 
    <h1 id="display"></h1> 
 
</body>

+0

ありがとう、私はそれを知らなかった。乾杯 – Kingsfull123

関連する問題