[英]How to stop an event from firing twice?
I'm trying to make a Tic-Tac-Toe game to understand Dom Manipulation;我正在尝试制作井字游戏来了解 Dom 操作; I've placed an
eventListener()
on all the individual divs that are inside the #main-wrap
Div.我在
#main-wrap
div 内的所有单个div 上放置了一个eventListener()
。 These divs contain SVG's of one cross and one circle that both hold CSS value display:none
.这些 div 包含一个十字和一个圆圈的 SVG,它们都包含 CSS 值
display:none
。 How once a player chooses his move do I make sure that the selected div cannot be chosen again?一旦玩家选择了他的动作,我如何确保无法再次选择所选的 div? (Because if it does get chosen again it changes it from a cross to a circle).
(因为如果它再次被选中,它会从十字变为圆形)。 If you can figure it out and please help me.
如果你能弄清楚,请帮助我。
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";
}
}
You should try putting the code for drawing your shapes into an else block.您应该尝试将用于绘制形状的代码放入 else 块中。
function addSVGs(e) {
whosGo ++;
if(whosGo % 2 === 0) {
this.firstElementChild.classList = "cross-display";
this.firstElementChild.nextElementSibling.classList = "circle";
}else{
this.firstElementChild.classList = "cross";
this.firstElementChild.nextElementSibling.classList = "circle-display";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.