【JavaScript】配列のサンプル
styleプロパティの変更
ボタンを押すとテキストの色が変わる。
<(○○○)>
■HTML
1 2 3 |
ボタンを押すとテキストの色が変わる。</br> <div id="ufo"><(○○○)></div> <button onclick="changeColor()">click me!!</button> |
■JavaScript
1 2 3 4 5 6 7 8 9 10 |
<script> var colors = ["red", "blue", "yellow"]; var index = 0; function changeColor() { document.getElementById("ufo").style.color = colors[index]; if ( ++index >= colors.length ) { index = 0; } } </script> |
![]() |