九耶丨阁瑞钛伦特-井字棋html5代码

你想了解关于井字棋(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")。以上代码只是一个基本示例,你可以根据需要进行扩展和美化。

相关推荐
站在风口的猪11087 分钟前
《前端面试题:前端布局全面解析(圣杯布局、双飞翼布局等)》
前端·css·html·css3·html5
站在风口的猪11081 天前
《前端面试题:HTML5、CSS3、ES6新特性》
前端·css3·html5
站在风口的猪11081 天前
《前端面试题:CSS有哪些单位!》
前端·css·html·css3·html5
xhload3d2 天前
图扑软件 | 带你体验 Low Poly 卡通三维世界
物联网·3d·智慧城市·html5·webgl·数字孪生·可视化·工业互联网·三维建模·工控·轻量化·中国风·卡通动画·写实风格·科技风·low poly
航Hang*3 天前
WEBSTORM前端 —— 第3章:移动 Web —— 第4节:移动适配-VM
前端·笔记·edge·less·css3·html5·webstorm
航Hang*3 天前
WEBSTORM前端 —— 第3章:移动 Web —— 第2节:空间转换、转化
前端·笔记·程序人生·edge·css3·html5·webstorm
_CodePencil_3 天前
CSS专题之水平垂直居中
前端·css·html·css3·html5
航Hang*4 天前
WEBSTORM前端 —— 第3章:移动 Web —— 第3节:移动适配
程序人生·edge·less·css3·html5·webstorm·rem
一只鱼^_4 天前
用JS实现植物大战僵尸(前端作业)
javascript·css·vscode·游戏引擎·游戏程序·html5·动画
椰椰椰耶4 天前
[网页五子棋][匹配模块]前后端交互接口(消息推送机制)、客户端开发(匹配页面、匹配功能)
java·spring boot·json·交互·html5·web