如何更改我的 javascript,以便只有选定的按钮为红色或绿色

我知道标题有点奇怪,但我不知道该怎么表达。我正在做的多项选择测试有一个重试按钮,如果你回答错误,如果你回答正确,则有一个下一步按钮,但后来我意识到,如果你回答错误,你实际上可以看到正确答案按钮以绿色和下次您回答时只需单击该按钮即可。所以我只希望背景和选定的按钮变成红色,如果你点击错误的按钮,其他按钮应该保持蓝色......我该怎么做?如果您回答正确,那么它可能会以现在的方式显示。如果这些都没有意义,那么请查看代码片段,我很确定它会解决问题。编辑:我更新了之前忘记更改的代码段中正确和错误的内容



<!DOCTYPE html>

<html>

  <!DOCTYPE html>

  <html>

  <head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <link rel="stylesheet" href="styles.css">

    <script defer src="script.js"></script>

     <title>Quiz App</title>

  </head>

  <body>

    </div>

    <div class="container">

      <div id="question-container" class="hide">

        <div id="question">Question</div>

        <div id="answer-buttons" class="btn-grid">

          <button class="btn">Answer 1</button>

          <button class="btn">Answer 2</button>

          <button class="btn">Answer 3</button>

          <button class="btn">Answer 4</button>

        </div> 

      </div>

      <div class="container1">

        <div id="startmsgcontainer" class="hide"></div>

          <div id="startmsg">Adventure Into The Human Immune System</div>

          </div>

      <div class="controls">

        <button id="start-btn" class="start-btn btn">Start!</button>

        <button id="next-btn" class="next-btn btn hide">Next</button>

        <button id="end-btn" class="end-btn btn hide">End (this will close the current tab)</button>

        <button id="try-btn" class="try-btn btn hide">Try again!</button>

      </div>

    </div>

    <div class="wrapper">

        <img src="img/uni.png" alt="image">

    </div>

    <div class="hide">

      <div id="imgcontainer">hello</div>

      <div id="image1" class="hide">

        <img src="img/wantedvirus.png" alt="image1">

    </div>

    </div>

   </div> 

    <div id="particles-js"></div>

    <script src="particles.js"></script>

    <script src="app.js"></script>

  </body>

  </html>


犯罪嫌疑人X
浏览 74回答 1
1回答

繁花如伊

不要setStatusClass()在所有按钮上调用,只需在selectedButton.const startButton = document.getElementById('start-btn')const nextButton = document.getElementById('next-btn')const questionContainerElement = document.getElementById('question-container')const questionElement = document.getElementById('question')const image1 = document.getElementById('image1')const answerButtonsElement = document.getElementById('answer-buttons')const startwords = document.getElementById('startmsg')const endbutton = document.getElementById('end-btn')const trybutton = document.getElementById('try-btn')const score = document.getElementById('score')let shuffledQuestions, currentQuestionIndexstartButton.addEventListener('click', startGame)nextButton.addEventListener('click', () => {&nbsp; currentQuestionIndex++&nbsp; setNextQuestion()})endbutton.addEventListener('click', () => {&nbsp; window.top.close()})trybutton.addEventListener('click', setNextQuestion)function startGame() {&nbsp; startButton.classList.add('hide')&nbsp; startwords.classList.add('hide')&nbsp; shuffledQuestions = questions.slice()&nbsp; questionContainerElement.classList.remove('hide')&nbsp; currentQuestionIndex = 0&nbsp; setNextQuestion()}function setNextQuestion() {&nbsp; resetState()&nbsp; showQuestion(shuffledQuestions[currentQuestionIndex])}function showQuestion(question) {&nbsp; questionElement.innerText = question.question&nbsp; question.answers.forEach(answer => {&nbsp; &nbsp; const button = document.createElement('button')&nbsp; &nbsp; button.innerText = answer.text&nbsp; &nbsp; button.classList.add('btn')&nbsp; &nbsp; if (answer.correct) {&nbsp; &nbsp; &nbsp; button.dataset.correct = answer.correct&nbsp; &nbsp; }&nbsp; &nbsp; button.addEventListener('click', selectAnswer)&nbsp; &nbsp; answerButtonsElement.appendChild(button)&nbsp; })}function resetState() {&nbsp; clearStatusClass(document.body)&nbsp; nextButton.classList.add('hide')&nbsp; while (answerButtonsElement.firstChild) {&nbsp; &nbsp; answerButtonsElement.removeChild(answerButtonsElement.firstChild)&nbsp; }&nbsp; trybutton.classList.add('hide')}function selectAnswer(e) {&nbsp; const selectedButton = e.target&nbsp; const correct = selectedButton.dataset.correct&nbsp; setStatusClass(document.body, correct)&nbsp; setStatusClass(selectedButton, correct);&nbsp; if(correct){&nbsp; &nbsp; if (shuffledQuestions.length > currentQuestionIndex + 1) {&nbsp; &nbsp; &nbsp; nextButton.classList.remove('hide')&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; endbutton.classList.remove('hide')&nbsp; &nbsp; }&nbsp; } else{&nbsp; &nbsp; &nbsp;trybutton.classList.remove('hide')&nbsp; }}function setStatusClass(element, correct) {&nbsp; clearStatusClass(element)&nbsp; if (correct) {&nbsp; &nbsp; element.classList.add('correct')&nbsp; }&nbsp; else {&nbsp; &nbsp; element.classList.add('wrong')&nbsp; }}function clearStatusClass(element) {&nbsp; element.classList.remove('correct')&nbsp; element.classList.remove('wrong')}const questions = [&nbsp; {&nbsp; &nbsp; question: 'What is 1+2?',&nbsp; &nbsp; answers: [&nbsp; &nbsp; &nbsp; { text: '3', correct: true },&nbsp; &nbsp; &nbsp; { text: '4', correct: false },&nbsp; &nbsp; &nbsp; { text: '5', correct: false },&nbsp; &nbsp; &nbsp; { text: '6', correct: false }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; question: 'What is 2-2?',&nbsp; &nbsp; answers: [&nbsp; &nbsp; &nbsp; { text: '0', correct: true },&nbsp; &nbsp; &nbsp; { text: '4', correct: false },&nbsp; &nbsp; &nbsp; { text: '5', correct: false },&nbsp; &nbsp; &nbsp; { text: '7', correct: false },&nbsp; &nbsp; ]&nbsp; },&nbsp;&nbsp;&nbsp;]*, *::before, *::after {&nbsp; box-sizing: border-box;&nbsp; font-family: cursive,&nbsp; 'Times New Roman', Times, serif}#particles-js {&nbsp; position: absolute;&nbsp; width: 100%;&nbsp; height: 100%;&nbsp; background-repeat: no-repeat;&nbsp; background-size: cover;&nbsp; background-position: 50% 50%;&nbsp; z-index: 1;}:root {&nbsp; --hue-neutral: 200;&nbsp; --hue-wrong: 0;&nbsp; --hue-correct: 145;}body {&nbsp; --hue: var(--hue-neutral);&nbsp; padding: 0;&nbsp; margin: 0;&nbsp; display: flex;&nbsp; width: 100vw;&nbsp; height: 100vh;&nbsp; justify-content: center;&nbsp; align-items: center;&nbsp; background-color: hsl(var(--hue), 100%, 20%);}body.correct {&nbsp; --hue: var(--hue-correct);}body.wrong {&nbsp; --hue: 0;}.container {&nbsp; width: 800px;&nbsp; max-width: 80%;&nbsp; background-color: white;&nbsp; border-radius: 5px;&nbsp; padding: 10px;&nbsp; box-shadow: 0 0 10px 2px;&nbsp; z-index: 2;}&nbsp;.btn-grid {&nbsp; display: grid;&nbsp; grid-template-columns: repeat(2, auto);&nbsp; gap: 10px;&nbsp; margin: 20px 0;}.btn {&nbsp; --hue: var(--hue-neutral);&nbsp; border: 1px solid hsl(var(--hue), 100%, 30%);&nbsp; background-color: hsl(var(--hue), 100%, 50%);&nbsp; border-radius: 5px;&nbsp; padding: 5px 10px;&nbsp; color: white;&nbsp; outline: none;}.btn:hover {&nbsp; border-color: black;}.btn.correct {&nbsp; --hue: var(--hue-correct);&nbsp; color: black;}.btn.wrong {&nbsp; --hue: var(--hue-wrong);}.next-btn {&nbsp; font-size: 1.5rem;&nbsp; font-weight: bold;&nbsp; padding: 10px 20px;&nbsp; align-items: flex-end;&nbsp; --hue: 245;}.start-btn {&nbsp; font-size: 1.5rem;&nbsp; font-weight: bold;&nbsp; padding: 10px 20px;&nbsp; --hue: 245;}.end-btn {&nbsp; font-size: 1.5rem;&nbsp; font-weight: bold;&nbsp; padding: 10px 20px;&nbsp; --hue: 245;}.try-btn {&nbsp; font-size: 1.5rem;&nbsp; font-weight: bold;&nbsp; padding: 10px 20px;&nbsp; --hue: 245;}.container1 {&nbsp; display: flex;&nbsp; justify-content: center;&nbsp; align-items: center;&nbsp;&nbsp; font-family: Arial;&nbsp; font-size: xx-large;&nbsp; padding: 10px 10px;&nbsp;&nbsp;}.container2 {&nbsp;&nbsp; width: 800px;&nbsp; max-width: 80%;&nbsp; background-color: white;&nbsp; border-radius: 5px;&nbsp; padding: 10px;&nbsp; box-shadow: 0 0 10px 2px;&nbsp; top:37%;&nbsp; position:absolute;&nbsp; display: flex;}.controls {&nbsp; display: flex;&nbsp; justify-content: center;&nbsp; align-items: center;&nbsp;}.hide {&nbsp; display: none;}.wrapper {&nbsp;&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; top: 0px;&nbsp; &nbsp; right: 0px;}<!DOCTYPE html><html>&nbsp; <!DOCTYPE html>&nbsp; <html>&nbsp; <head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; &nbsp; <link rel="stylesheet" href="styles.css">&nbsp; &nbsp; <script defer src="script.js"></script>&nbsp; &nbsp; &nbsp;<title>Quiz App</title>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="container">&nbsp; &nbsp; &nbsp; <div id="question-container" class="hide">&nbsp; &nbsp; &nbsp; &nbsp; <div id="question">Question</div>&nbsp; &nbsp; &nbsp; &nbsp; <div id="answer-buttons" class="btn-grid">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn">Answer 1</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn">Answer 2</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn">Answer 3</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn">Answer 4</button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="container1">&nbsp; &nbsp; &nbsp; &nbsp; <div id="startmsgcontainer" class="hide"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div id="startmsg">Adventure Into The Human Immune System</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="controls">&nbsp; &nbsp; &nbsp; &nbsp; <button id="start-btn" class="start-btn btn">Start!</button>&nbsp; &nbsp; &nbsp; &nbsp; <button id="next-btn" class="next-btn btn hide">Next</button>&nbsp; &nbsp; &nbsp; &nbsp; <button id="end-btn" class="end-btn btn hide">End (this will close the current tab)</button>&nbsp; &nbsp; &nbsp; &nbsp; <button id="try-btn" class="try-btn btn hide">Try again!</button>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="wrapper">&nbsp; &nbsp; &nbsp; &nbsp; <img src="img/uni.png" alt="image">&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="hide">&nbsp; &nbsp; &nbsp; <div id="imgcontainer">hello</div>&nbsp; &nbsp; &nbsp; <div id="image1" class="hide">&nbsp; &nbsp; &nbsp; &nbsp; <img src="img/wantedvirus.png" alt="image1">&nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp;</div>&nbsp;&nbsp; &nbsp; <div id="particles-js"></div>&nbsp; &nbsp; <script src="particles.js"></script>&nbsp; &nbsp; <script src="app.js"></script>&nbsp; </body>&nbsp; </html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript