TypeError: Cannot read property 'target' of undefined, error received for checking if a div has not already been filled
TypeError: Cannot read property 'target' of undefined, error received for checking if a div has not already been filled
尝试为作业制作井字棋,我对 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)
并且return如果没有给定参数,当没有事件等时。
function addMarker(e){
if(!e) return;
if ...
尝试为作业制作井字棋,我对 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)
并且return如果没有给定参数,当没有事件等时。
function addMarker(e){
if(!e) return;
if ...