<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Tic Tac Toe</title>
    <style>
        #game{
            width: 300px;
            margin: 20px auto;
        }
        .box{
            width: 100px;
            height: 100px;
            border: 1px solid #000;
            float: left;
            font-size: 60px;
            line-height: 100px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="game">
        <div class="box" id="box-1"></div>
        <div class="box" id="box-2"></div>
        <div class="box" id="box-3"></div>
        <div class="box" id="box-4"></div>
        <div class="box" id="box-5"></div>
        <div class="box" id="box-6"></div>
        <div class="box" id="box-7"></div>
        <div class="box" id="box-8"></div>
        <div class="box" id="box-9"></div>
    </div>
    <script>
        const boxes = document.querySelectorAll('.box');
        let turn = 'X';
        for (let i = 0; i < boxes.length; i++){
            boxes[i].addEventListener('click', function(){
                if(this.innerHTML == ''){
                    this.innerHTML = turn;
                    if(turn == 'X'){
                        turn = 'O';
                    }
                    else{
                        turn = 'X';
                    }
                }
            });
        }
    </script>
</body>
</html>
Tic Tac Toe Game: Play Online Now | HTML Code

原文地址: https://www.cveoy.top/t/topic/lnlH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录