如何解决TypeError:无法读取未定义的属性“ 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 ...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。