【JavaScript】○×ゲーム
マスをクリックすると、交互に○×が表示される。
ソースコード
■HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<table class="gameTable"> <tr> <td onclick="check(this)"></td> <td onclick="check(this)"></td> <td onclick="check(this)"></td> </tr> <tr> <td onclick="check(this)"></td> <td onclick="check(this)"></td> <td onclick="check(this)"></td> </tr> <tr> <td onclick="check(this)"></td> <td onclick="check(this)"></td> <td onclick="check(this)"></td> </tr> </table> |
■CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
.gameTable { width:300px; height:300px; border: 1px #000000 solid; } .gameTable tr { width:32%; height:32%; border: 1px #000000 solid; } .gameTable tr td { font-size:40px; width:32%; height:32%; border: 1px #000000 solid; text-align:center; } |
■JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//○×ゲーム var i=0; var content; function check(element) { if ( element.textContent != "" ) { window.confirm("そのマスは指定できません。"); return; } if ( i++ %2 == 0 ) { content = "○"; element.style.color = "red"; } else { content = "×"; element.style.color = "blue"; } element.textContent = content; } |
![]() |