【发布时间】:2021-04-22 01:14:36
【问题描述】:
我正在尝试制作井字游戏以了解 Dom 操纵;
我在#main-wrap div 内的所有单个div 上放置了eventListener()。
这些 div 包含一个十字和一个圆圈的 SVG,它们都保存 CSS 值 display:none。
一旦玩家选择了他的动作,我如何确保无法再次选择所选的 div? (因为如果它再次被选中,它会从十字变为圆形)。
如果你能弄清楚,请帮助我。
const section1 = document.querySelector('.section-one');
const mainDiv = document.getElementById('main-wrap');
let whosGo = 0;
for (prop of mainDiv.children) {
prop.addEventListener('click',addSVGs);
}
function addSVGs(e) {
this.firstElementChild.classList = "cross";
this.firstElementChild.nextElementSibling.classList = "circle-display";
whosGo ++;
if(whosGo % 2 === 0) {
this.firstElementChild.classList = "cross-display";
this.firstElementChild.nextElementSibling.classList = "circle";
}
}
【问题讨论】:
标签: javascript css events logic dom-events