类型错误:无法读取未定义的属性'target',收到检查 div 是否尚未填充的错误



试图为一项任务制作TicTacToe,我对JavaScript还很陌生,任何帮助都将不胜感激:

//onlick event listner
document.getElementById('board').addEventListener('click', handleTurn);
function addMarker(e){
if(e.target.textContent == fill.EMPTY){ //error shows up for this line specifically
count++;
if (count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
playerTurn = fill.X;
document.getElementById("switch").innerHTML = "It's O's turn";  
}   
else {
playerTurn = fill.O;
document.getElementById("switch").innerHTML = "It's X turn";

}
e.target.textContent = playerTurn;
}
else{
alert('cannot click here, choose a free square!');
}
e.stopPropagation();
handleTurn();
render();
}

检查目标是否存在。

if(e.target && e.target.textContent == fill.EMPTY)

如果没有给定参数,当没有事件或类似事件时返回。

function addMarker(e){
if(!e) return;
if ...

最新更新