简体   繁体   English

classList.add已停止我的代码运行。 我不知道为什么。 你是否可以?

[英]classList.add has stopped my code from running. I can't figure out why. Can you?

 var p1Button = document.querySelector("#p1"); var p2Button = document.getElementById("p2"); var p1Display = document.querySelector("#p1Display"); var p2Display = document.querySelector("#p2Display"); var p1Score = 0; var p2Score = 0; var gameOver = false; var winningScore = 5; p1Button.addEventListener("click", function(){ if(!gameOver){ p1Score++; if (p1Score === winningScore) p1Display.classList.add("winner"); gameOver = true; } p1Display.textContent = p1Score; }); p2Button.addEventListener("click", function(){ if(!gameOver){ p2Score++; if (p2Score === winningScore) p2Display.classList.add("winner"); gameOver = true; } p2Display.textContent = p2Score; }); 
 .winner { color: green; } 
 <h1><span id = "p1Display">0</span> to <span id = "p2Display">0</span></h1> <p>Playing to: 5</p> <input type="number"> <button id="p1" type="button">Player One</button> <button id="p2" type="button">Player Two</button> <button id="reset" type="button">Reset</button> 

The p2Display.classList.add("winner"); p2Display.classList.add("winner"); lines on my two functions has stopped my code from working. 我的两个函数上的代码行使我的代码无法正常工作。

If you remove it the code works fine. 如果将其删除,则代码可以正常工作。

However, I need that line of code in order to complete this game. 但是,我需要该行代码才能完成此游戏。

Is there a reason why it's stopping my game from working? 有什么理由会阻止我的游戏正常运行?

I can't seem to figure it out. 我似乎无法弄清楚。

You are missing the opening and closing brackets around the statements in the if block: 您缺少if块中语句前后的左括号和右括号:

if (p1Score === winningScore)
    p1Display.classList.add("winner");
    gameOver = true;

So, only the first statement is considered part of the if block, while the second statement gameOver = true; 因此,只有第一个语句被视为if块的一部分,而第二个语句gameOver = true; executes regardless. 无论执行。 As a result, the game gets over with the first click event. 结果,游戏结束了第一次单击事件。

Here's the updated snippet with opening and closing brackets added around the if block: 这是更新的代码段,在if块周围添加了左括号和右括号:

 var p1Button = document.querySelector("#p1"); var p2Button = document.getElementById("p2"); var p1Display = document.querySelector("#p1Display"); var p2Display = document.querySelector("#p2Display"); var p1Score = 0; var p2Score = 0; var gameOver = false; var winningScore = 5; p1Button.addEventListener("click", function() { if (!gameOver) { p1Score++; if (p1Score === winningScore) { p1Display.classList.add("winner"); gameOver = true; } } p1Display.textContent = p1Score; }); p2Button.addEventListener("click", function() { if (!gameOver) { p2Score++; if (p2Score === winningScore) { p2Display.classList.add("winner"); gameOver = true; } } p2Display.textContent = p2Score; }); 
 .winner { color: green; } 
 <head> <title>Score Keeper</title> <link rel="stylesheet" type="text/css" href="scoreKeeper.css"> </head> <body> <h1><span id="p1Display">0</span> to <span id="p2Display">0</span></h1> <p>Playing to: 5</p> <input type="number"> <button id="p1" type="button">Player One</button> <button id="p2" type="button">Player Two</button> <button id="reset" type="button">Reset</button> <script type="text/javascript" src="scoreKeeper.js"></script> </body> 

You messed up with your curly braces. 你弄乱了花括号。

you need to remove the opening curly brace here: if(!gameOver){ 您需要在此处删除左花括号:if(!gameOver){

and you need to add a curly brace: if (p2Score === winningScore) (in both eventlisteners) 并且您需要添加一个大括号:if(p2Score === winningScore)(在两个事件侦听器中)

p2Button.addEventListener("click", function(){
if(!gameOver)
    p2Score++;

if (p2Score === winningScore){
    p2Display.classList.add("winner");
    gameOver = true;
}

p2Display.textContent = p2Score;})

otherwise your execution order of the lines is messed up 否则,您的行的执行顺序会混乱

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (jQuery)&#39;table.class&#39;不起作用,我不知道为什么。 它也会选择其他类的表 - (JQuery) 'table.class' doesn't work and i can't figure out why. It selects tables with other classes too 我的 javascript 验证刚刚停止工作,我不知道为什么 - My javascript validation just stopped working and I can't figure out why 为什么classList.add不能用于html onClick - Why doesn't classList.add work for html onClick 为什么 &#39;&#39;classList.add()&#39;&#39; 方法在我的 Vuetify 应用程序中不起作用? - Why ''classList.add()'' method does not work in my Vuetify app? 当classlist.add位于另一个元素上时,如何更改元素的样式? - How can I change the style of an element when the classlist.add is on another element? 使用 classList.add 时如何忽略空格? - How can I ignore white space when using classList.add? 我的 javascript 代码不会将元素推送到我的数组,我不知道为什么? - My javascript code won't push elements to my array and I can't figure out why? 为什么 ClassList.Add() 没有应用样式? - Why Is Style Not Getting Applied with ClassList.Add()? 我有编程经验,但是对Javascript还是陌生的,无法弄清楚为什么我的代码无法运行 - I'm experienced with programming but new to Javascript, can't figure out why my code won't run 我的 autorole 代码不起作用,我不明白为什么 - My code for autorole doesn't work and I can't figure out why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM