我在做一个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
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
<!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
/*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;
}发布于 2022-11-26 19:38:27
在您的resetGame函数中,您应该遍历所有单元格并删除它们的内容,而不是删除整个游戏板内容。
for(let i =0;i < gameDisplay.children.length;i++) {
gameDisplay.children[i].innerHTML = '';
}https://stackoverflow.com/questions/74585025
复制相似问题