2017-06-19 10 views
0

upの矢印をクリックするたびに空の段落で1ずつインクリメントするコードがあります。私はJavascriptを使用していますがonclickイベントハンドラが動作していないよう:Javascript img onclickは効果がありませんか?

<head> 
 

 
    <script> 
 
    var incremented = 0; 
 

 
    function myfunction() { 
 
     document.getElementById("uparrow").innerHTML = incremented + 1; 
 
    } 
 
    </script> 
 

 
</head> 
 

 
<body> 
 
    <img id="uparrow" src="uparrow.png" alt="Up" onclick="myfunction()"> 
 
    <p id="incremented></p> 
 
    </body>

+1

'のdocument.getElementById( "上矢印")は' 'のdocument.getElementById( "インクリメント")されなければならない' –

+1

そして、あなたは 'incremented' varableをインクリメントすることはありません。 'incremented = incremented + 1'のようなものを追加してください –

答えて

5

入力した何かを欠場します。 uparrowではなく、incremented要素に新しい値を挿入する必要があります。また、毎回あなたの価値を増やしてください。

img{ 
 
    height: 50px; 
 
    width: 50px; 
 
}
<head> 
 

 
<script> 
 
    var value = 0; 
 

 
    function myIncrementfunction(){ 
 
     document.getElementById("incremented").innerHTML = ++value; 
 
    } 
 
    
 
    function myDecrementfunction(){ 
 
     document.getElementById("incremented").innerHTML = --value; 
 
    } 
 
    
 
</script> 
 

 
</head> 
 

 
<body> 
 
<img id="uparrow" src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-up-c-128.png" alt="Up" onclick="myIncrementfunction()"> 
 
<img id="downarrow" src="http://iconizer.net/files/DefaultIcon_ver_0.11/orig/arrow-alt-down.png" alt="Up" onclick="myDecrementfunction()"> 
 
<p id="incremented">0</p> 
 
</body>

+0

ありがとうございます。しかし、クリックするたびに-1をする別の「下向き」矢印を追加したいのですが? – KoyaCho

+0

私の呪文を編集します。参照してください。 –

1

あなたは間違った選択を持っています。あなたは毎回onclickを増やす必要があります。

<head> 
 

 
</head> 
 

 
<body> 
 
    <img id="uparrow" src="uparrow.png" alt="Up" onclick="myfunction()"> 
 
    <p id="incremented"></p> 
 

 
    <script> 
 
    var incremented = 0; 
 

 
    function myfunction() { 
 
     document.getElementById("incremented").innerHTML = ++incremented; 
 
    } 
 
    </script> 
 

 
</body>

+0

おそらく '++をインクリメントするだけです。 – Mikey

+0

@Mikeyがあなたの提案を追加しました。ありがとう。 –

関連する問題