2017-02-24 7 views
0

htmlのタグ内の要素の色を黒から赤に変更し、再度黒に戻す必要があります。私はそれが私が要素にクラスを追加することで、このアプローチをして、CSSを通じて色のスタイルに、そのクラスを結ぶだろう1.ボタンを押したときにhtmlのタグの色を変更する方法はありますか?

<html> 
<head> 
<body> 
    <div id="e">0</div> 
    <input type="button" onclick="foo()" value="Ok"> 

    <script> 
     var e = 0 
     function foo(){ 
      kak = document.getElementById('e').innerHTML = e += 1 
     } 
    </script> 

</body> 
</html> 
+6

'のdocument.getElementById( "E")style.color = "赤" です;' – artemisian

答えて

2

を追加するには、簡単なボタンがあります。ここで私が話していることの例です。 。

<html> 
    <head> 
    <style> 
     #input-button { background-color: black; color: white; } 
     #input-button.red { background-color: red } 
    </style> 
    </head> 
    <body> 
    <div id="e">0</div> 
    <input id="input-button" type="button" onclick="foo()" value="Ok"> 

    <script> 
     var e = 0 
     function foo(){ 
     var el = document.getElementById('e'); 
     el.innerHTML = e += 1; 

     var button = document.getElementById('input-button'); 
     button.classList.add('red'); 

     // setTimeout begins a timer, and I pass 500ms. To 
     // make this longer, increase the number below 
     setTimeout(function(){ 
      button.classList.remove('red'); 
     }, 500) 

     } 
    </script> 
    </body> 
</html> 

ここ例

jsfiddle
関連する問題