2016-12-22 8 views
2

なぜ私のonclickは以前に実行されたjs関数をオーバーライドしませんか?この関数は、PHPのエコー内からトリガされます。関数が実行され、イメージとその幅と高さが変更されます。後で私のボタンをクリックすると、イメージが再び変化しますが、イメージだけが変化し、幅と高さは変わりません。いくつかのPHP内部 なぜ私のonclickは先に実行されたjs関数をオーバーライドしませんか?

<script> 
    function helmfunc() {    
       document.getElementById("helmplace").src = "helmimg/s_helmet.png"; //executes        
    document.getElementById("helmplace").style.width = "22px"; //executes 
    document.getElementById("helmplace").style.height = "31px"; //executes      
       } 
</script> 
<img id="helmplace" name="showh" src="redx3.png" width="25" height="30" /> 
<span onclick="showh.src = x_helm.src; showh.width = x_helm.width; showh.height = x_helm.height; helm.value = 'x helm'"> 
       <il> 
        <img id="xhelmid" name="x_helm" src="helmimg/x_helmet.png" width="22" height="31" />x Helm 
       </il> 
      </span> 

:(遠い体内でダウン)の代わりにイベントリスナーを使用している、onclickをクリック上のコードを実行するために、一般的に受け入れられる方法を使用しての

echo '<script type="text/javascript"> slayerhelmfunc(); </script>'; 
+1

イベントリスナーの代わりに、onclickのを使用してください。 –

+0

yupが今働いていて、これほど長い間これに執着していたことがちょっとばかげていた:P –

+3

@ThomasJuranek働いているようだから、あなたのコメントを答えに変えてアップアップして受け入れることができますか? – techturtle

答えて

3

。次に例を示します。

<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
\t <input type="number" id="inputInteger" /> 
 
\t <input type="submit" id="btnSubmit" text="Submit" /> 
 
    
 
    <p id="result"></p> 
 

 
\t <script type = "text/javascript"> 
 
     // Event Listener that Runs "Compute()" on a Click 
 
     document.getElementById("btnSubmit").addEventListener("click", compute); 
 

 
     // Gets Input Value and Displays it in a <p> 
 
     function compute() { 
 
      var day = document.getElementById("inputInteger").value; 
 
      document.getElementById("result").innerHTML = day; 
 
     } 
 
    </script> 
 
</body> 
 
</html>

関連する問題