你想了解關于井字棋(Tic-Tac-Toe)的HTML代碼嗎?以下是一個簡單的井子棋的HTML代碼示例:
<!DOCTYPE html>
<html>
<head><title>Tic-Tac-Toe</title><style>.board {display: flex;flex-wrap: wrap;width: 300px;}.cell {width: 100px;height: 100px;border: 1px solid #000;display: flex;justify-content: center;align-items: center;font-size: 48px;cursor: pointer;}</style>
</head>
<body><div class="board"><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div></div><script>const cells = document.querySelectorAll('.cell');let currentPlayer = 'X';function handleCellClick(e) {const cell = e.target;if (cell.textContent === '') {cell.textContent = currentPlayer;currentPlayer = currentPlayer === 'X' ? 'O' : 'X';}}cells.forEach(cell => {cell.addEventListener('click', handleCellClick);});</script>
</body>
</html>
這段代碼創建了一個3x3的井字棋盤,使用了HTML、CSS和JavaScript。每個格子都是一個 <div>
元素,點擊格子時會在其內部顯示當前玩家的標記("X"或"O")。以上代碼只是一個基本示例,你可以根據需要進行擴展和美化。