gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot set property 'innerText' of null - possible syntax error

转载 作者:行者123 更新时间:2023-12-01 03:59:15 25 4
gpt4 key购买 nike

正在开发一个简单的 Tic Tac Toe 游戏来改善我的 JS,但我遇到了这个错误消息。我的“console.log(“消息”);”返回它应该显示的内容“游戏已准备好。玩家 X 轮到。”它是一个单一文件,我的脚本标签位于我的 </body> 上方标签。这是非常基本的。任何帮助,将不胜感激。

var Display = function(displayElement) {						// create 'class' to eliminate many 'innerText' usage 
var display = displayElement; // and makes it easy to port to say android
function setText(message) {
console.log(message);
display.innerText = message;
}

return {setMessage: setText};
};

function isValid(button) { //if text within then not valid; if 0 then valid; any
return button.innerText.length == 0; //characters then invalid
}

function checkForWinner(squares, players, currentTurn) {
if (squares[0].innerText == players[currentTurn] &&
squares[1].innerText == players[currentTurn] &&
squares[2].innerText == players[currentTurn])
return true;
if (squares[3].innerText == players[currentTurn] &&
squares[4].innerText == players[currentTurn] &&
squares[5].innerText == players[currentTurn])
return true;
if (squares[6].innerText == players[currentTurn] &&
squares[7].innerText == players[currentTurn] &&
squares[8].innerText == players[currentTurn])
return true;
if (squares[0].innerText == players[currentTurn] &&
squares[3].innerText == players[currentTurn] &&
squares[6].innerText == players[currentTurn])
return true;
if (squares[1].innerText == players[currentTurn] &&
squares[4].innerText == players[currentTurn] &&
squares[7].innerText == players[currentTurn])
return true;
if (squares[2].innerText == players[currentTurn] &&
squares[5].innerText == players[currentTurn] &&
squares[8].innerText == players[currentTurn])
return true;
if (squares[0].innerText == players[currentTurn] &&
squares[4].innerText == players[currentTurn] &&
squares[8].innerText == players[currentTurn])
return true;
if (squares[2].innerText == players[currentTurn] &&
squares[4].innerText == players[currentTurn] &&
squares[6].innerText == players[currentTurn])
return true;
}

function setMark(button, mark) {
button.innerText = mark;
}

function main() {
var squares = document.querySelectorAll("#game button"); // returns array of all the buttons
var players = ["X", "O"]; // text displayed in the button
var currentTurn = 0; // either will be 0 or 1
var isGameOver = false; // boolean either T or F
var display = new Display(document.querySelector("gameActionDisplay")); //reference to msg

display.setMessage("Game is ready. Player " + players[currentTurn] + " turn.");

for (var i = 0, len = squares.length; i<len; i++) { //for loop to iterate through squares; set local var len
squares[i].addEventListener("click", function() {
if (isGameOver)
return;

if(!isValid(this)) {
display.setMessage("Invalid move bud!");
} else {
setMark(this, players[currentTurn]);

isGameOver = checkForWinner(squares, players, currentTurn);

//Game is over

//Game is draw

//Game is not over yet; play on
currentTurn++; // mod remainder toggles between 1 and 0
currentTurn = currentTurn % 2;

display.setMessage("Player " + players[currentTurn] + " move");

}
});
}
}

main();
<body>
<div id="game">
<div>
<button></button>
<button></button>
<button></button>
</div>
<div>
<button></button>
<button></button>
<button></button>
</div>
<div>
<button></button>
<button></button>
<button></button>
</div>

<div id="gameActionDisplay"></div>


</div>
</body>

最佳答案

您需要通过 id 获取元素

document.querySelector("#gameActionDisplay") 

做点什么

干杯

关于javascript - 未捕获的类型错误 : Cannot set property 'innerText' of null - possible syntax error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42334434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com