首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何清除innerHTML内容的HTMLCollection?

如何清除innerHTML内容的HTMLCollection?
EN

Stack Overflow用户
提问于 2022-11-26 19:28:02
回答 1查看 25关注 0票数 0

我在做一个Tic Tac脚趾游戏。我的问题是在单击innerHTML时无法清除多个div (div类.cell)的resetButton。我想澄清的是gameDisplay的孩子们。

当我试图在我的chrome工具中定位单元格元素时,我得到了一个HTML:ScreenShot在这里

我在访问这些元素的innerHTML时遇到了困难。目前,我的resetGame()函数返回gameDisplay.innerHTML=“(第79行),这将删除父元素的所有内容,并完全删除网格(板)。我知道这还不够具体。因此,我尝试返回gameDisplay.children.innerHTML ="";但是在DOM或我的chrome工具控制台中什么都没有发生。

我如何访问这些div的innerHTML并使用resetGame函数删除其中的resetGame?

JS

代码语言:javascript
复制
const playerFactory = () => {
    const playTurn = (event, currentPlayer) => {
      const id = boardObject.cells.indexOf(event.target);
      boardObject.boardDisplay[id] = currentPlayer;
      boardObject.render();
    };
  
    return {
      playTurn
    };
  };
  
  // Gameboard object
  const boardObject = (() => {
    let boardDisplay = ['', '', '', '', '', '', '', '', ''];
    const cells = Array.from(document.querySelectorAll(".cell"));
    // displays the content of the boardArray
    const render = () => {
      boardDisplay.forEach((cells, idx) => {
        cells[idx].innerHTML = boardDisplay[idx];
      });
    };
    return {
      boardDisplay,
      render,
      cells
    };
  })();


  // Create Array from DOM
const boardArray = [...document.querySelectorAll(".cell")].map(cells=>cells.innerHTML);
console.log(boardArray);

// Display controller, shows which player is which
const displayController = (() => {
const playerOne = 'X';
const playerTwo = 'O';
const gameBoard = document.querySelector(".game-board");
let currentPlayer = playerOne;

const switchPlayer = () => {
    currentPlayer = currentPlayer === playerOne ? playerTwo : playerOne;
  }

  gameBoard.addEventListener("click", (event) => {
    if (event.target.classList.contains("cell")) {
      if (event.target.textContent === "") {
        event.target.textContent = currentPlayer;
        const indexOfClickedCell = Array.prototype.indexOf.call(document.querySelectorAll('.cell'), event.target);
        boardArray[indexOfClickedCell] = currentPlayer;
        switchPlayer();
             
   
      }
    }
  });
})();


const gameDisplay = document.getElementById("game-board");


// Define reset button function first
function resetGame(){

// Define result, reset the board
//if the board array index is equal to a character
if(boardArray.includes('X','O')){
    { return gameDisplay.innerHTML = "";
       
      }
}
else
  {;}
//and if the cells textContent is equal to a character
//return the result, which is the reset board function 
}

const resetButton = document.querySelector(".restart");
resetButton.addEventListener("click", (event) => {
    resetGame(event);
});

HTML

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://use.typekit.net/xtq6hun.css">
    <link rel="stylesheet" href="styles.css">
    <title>TIC-TAC-TOE</title>
</head>
<body>
    <main>
<div class="top-text">
<h1>TIC TAC TOE</h1>
<div id ="game-text">
</div>
</div>
<div class="game-board" id="game-board">
<div class="cell" id="cell-1"></div>
<div class="cell" id="cell-2"></div>
<div class="cell" id="cell-3"></div>
<div class="cell" id="cell-4"></div>
<div class="cell" id="cell-5"></div>
<div class="cell" id="cell-6"></div>
<div class="cell" id="cell-7"></div>
<div class="cell" id="cell-8"></div>
<div class="cell" id="cell-9"></div>
</div>
<div class="bottom-box">
<button class="restart">RESTART GAME</button>
</div>
    </main>
<script src="script.js" defer></script>
</body>
</html>

CSS

代码语言:javascript
复制
/*CSS RESET*/
* {
margin:0;
padding:0;
}

body {
background-color: #faf9fa;
}

main {
display: flex;
align-content: center;
flex-direction: column;
flex-wrap: nowrap;
justify-content: center;
align-items: center;
}

h1 {
font-family: blackest-text, sans-serif;
font-weight: 700;
font-style: normal;
font-size: 60px;
color: #38393a;
}

h2 {
font-family: elza, sans-serif;
font-weight: 500;
font-style: normal;
font-size: 20px;
color: #38393a;
}

.top-text {
border: none;
margin: 0.5em;
display: flex;
flex-direction: column;
align-items: center;
}

.game-board {
display: grid;
grid-template-columns: repeat(3, 1fr);
border: 1px solid #38393a;
margin: 0px auto;
background-color: #faf9fa;
margin-bottom: 2em;
}

.cell:hover {
background-color: rgb(221, 253, 228); 
}

.cell {
/*STYLING X'S AND O'S*/    
font-family: blackest-text, sans-serif;
font-weight: 700;
font-style: normal;
font-size: 100px;
color: #38393a;
/*STYLING INDIVUAL CELLS*/
min-height: 170px;
min-width: 170px;
border: 1px solid #38393a;
display: flex;
align-content: center;
flex-wrap: nowrap;
justify-content: center;
align-items: center;
}

img {
width: 90px;
color:#38393a;
}

.message-display {
border: none;
margin-bottom: 1em;
}

.restart{
font-family: elza, sans-serif;
font-weight: 500;
font-style: normal;
height: 3.5em;
width: 12em;
border-radius: 30px;
border: 1px solid #38393a;
background-color: #faf9fa;
padding: 1px;
}

.restart:hover{
background-color: rgb(221, 253, 228);
}

span {
    font-family: blackest-text, sans-serif;
    font-weight: 700;
    font-style: normal;
    font-size: 100px;
    color: #38393a;
    }

p {
    font-family: blackest-text, sans-serif;
    font-weight: 700;
    font-style: normal;
    font-size: 30px;
    color: #38393a;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-26 19:38:27

在您的resetGame函数中,您应该遍历所有单元格并删除它们的内容,而不是删除整个游戏板内容。

代码语言:javascript
复制
for(let i =0;i < gameDisplay.children.length;i++) {
    gameDisplay.children[i].innerHTML = '';
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74585025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档