简体   繁体   English

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

[英]How do I change my javascript so that only the selected button is coloured red or green

I know the title is a bit weird but I didn't know how else to phrase it.我知道标题有点奇怪,但我不知道如何表达它。 The multiple choice test I am doing has an try again button if you get it answer wrong and a next button if you answer correctly, but then I realized that if you answer something wrong, you can actually see the correct answer button highlighted in green and just click that the next time you are answering.我正在做的多项选择测试有一个“重试”按钮,如果你答错了,还有一个“下一步”按钮,如果你回答正确,但后来我意识到,如果你回答错了,你实际上可以看到以绿色突出显示的正确答案按钮,下次回答时只需单击它。 So I want only the background and the selected button to turn red and the other buttons should stay blue if you click the wrong button... How do I do this?所以我只希望背景和选定的按钮变成红色,如果你点击了错误的按钮,其他按钮应该保持蓝色......我该怎么做? If you answer correctly the then it could be displayed the way it is right now.如果您回答正确,那么它可能会以现在的方式显示。 If none of this makes sense then look at the snippet, I'm pretty sure it will clear things up.如果这一切都没有意义,那么请查看代码段,我很确定它会解决问题。 Edit: I updated the correct and wrong thing in the snippet which I previously forgot to change编辑:我更新了之前忘记更改的片段中正确和错误的内容

 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, currentQuestionIndex startButton.addEventListener('click', startGame) nextButton.addEventListener('click', () => { currentQuestionIndex++ setNextQuestion() }) endbutton.addEventListener('click', () => { window.top.close() }) trybutton.addEventListener('click', setNextQuestion) function startGame() { startButton.classList.add('hide') startwords.classList.add('hide') shuffledQuestions = questions.slice() questionContainerElement.classList.remove('hide') currentQuestionIndex = 0 setNextQuestion() } function setNextQuestion() { resetState() showQuestion(shuffledQuestions[currentQuestionIndex]) } function showQuestion(question) { questionElement.innerText = question.question question.answers.forEach(answer => { const button = document.createElement('button') button.innerText = answer.text button.classList.add('btn') if (answer.correct) { button.dataset.correct = answer.correct } button.addEventListener('click', selectAnswer) answerButtonsElement.appendChild(button) }) } function resetState() { clearStatusClass(document.body) nextButton.classList.add('hide') while (answerButtonsElement.firstChild) { answerButtonsElement.removeChild(answerButtonsElement.firstChild) } trybutton.classList.add('hide') } function selectAnswer(e) { const selectedButton = e.target const correct = selectedButton.dataset.correct setStatusClass(document.body, correct) Array.from(answerButtonsElement.children).forEach(button => { setStatusClass(button, button.dataset.correct) }) if(correct){ if (shuffledQuestions.length > currentQuestionIndex + 1) { nextButton.classList.remove('hide') } else { endbutton.classList.remove('hide') } } else{ trybutton.classList.remove('hide') } } function setStatusClass(element, correct) { clearStatusClass(element) if (correct) { element.classList.add('correct') } else { element.classList.add('wrong') } } function clearStatusClass(element) { element.classList.remove('correct') element.classList.remove('wrong') } const questions = [ { question: 'What is 1+2?', answers: [ { text: '3', correct: true }, { text: '4', correct: false }, { text: '5', correct: false }, { text: '6', correct: false } ] }, { question: 'What is 2-2?', answers: [ { text: '0', correct: true }, { text: '4', correct: false }, { text: '5', correct: false }, { text: '7', correct: false }, ] }, ]
 *, *::before, *::after { box-sizing: border-box; font-family: cursive, 'Times New Roman', Times, serif } #particles-js { position: absolute; width: 100%; height: 100%; background-repeat: no-repeat; background-size: cover; background-position: 50% 50%; z-index: 1; }:root { --hue-neutral: 200; --hue-wrong: 0; --hue-correct: 145; } body { --hue: var(--hue-neutral); padding: 0; margin: 0; display: flex; width: 100vw; height: 100vh; justify-content: center; align-items: center; background-color: hsl(var(--hue), 100%, 20%); } body.correct { --hue: var(--hue-correct); } body.wrong { --hue: 0; }.container { width: 800px; max-width: 80%; background-color: white; border-radius: 5px; padding: 10px; box-shadow: 0 0 10px 2px; z-index: 2; }.btn-grid { display: grid; grid-template-columns: repeat(2, auto); gap: 10px; margin: 20px 0; }.btn { --hue: var(--hue-neutral); border: 1px solid hsl(var(--hue), 100%, 30%); background-color: hsl(var(--hue), 100%, 50%); border-radius: 5px; padding: 5px 10px; color: white; outline: none; }.btn:hover { border-color: black; }.btn.correct { --hue: var(--hue-correct); color: black; }.btn.wrong { --hue: var(--hue-wrong); }.next-btn { font-size: 1.5rem; font-weight: bold; padding: 10px 20px; align-items: flex-end; --hue: 245; }.start-btn { font-size: 1.5rem; font-weight: bold; padding: 10px 20px; --hue: 245; }.end-btn { font-size: 1.5rem; font-weight: bold; padding: 10px 20px; --hue: 245; }.try-btn { font-size: 1.5rem; font-weight: bold; padding: 10px 20px; --hue: 245; }.container1 { display: flex; justify-content: center; align-items: center; font-family: Arial; font-size: xx-large; padding: 10px 10px; }.container2 { width: 800px; max-width: 80%; background-color: white; border-radius: 5px; padding: 10px; box-shadow: 0 0 10px 2px; top:37%; position:absolute; display: flex; }.controls { display: flex; justify-content: center; align-items: center; }.hide { display: none; }.wrapper { position: absolute; top: 0px; right: 0px; }
 <,DOCTYPE html> <html lang="en"> <.DOCTYPE html> <html lang="en"> <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>

Don't call setStatusClass() on all the buttons, just call it on the selectedButton .不要在所有按钮上调用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, currentQuestionIndex startButton.addEventListener('click', startGame) nextButton.addEventListener('click', () => { currentQuestionIndex++ setNextQuestion() }) endbutton.addEventListener('click', () => { window.top.close() }) trybutton.addEventListener('click', setNextQuestion) function startGame() { startButton.classList.add('hide') startwords.classList.add('hide') shuffledQuestions = questions.slice() questionContainerElement.classList.remove('hide') currentQuestionIndex = 0 setNextQuestion() } function setNextQuestion() { resetState() showQuestion(shuffledQuestions[currentQuestionIndex]) } function showQuestion(question) { questionElement.innerText = question.question question.answers.forEach(answer => { const button = document.createElement('button') button.innerText = answer.text button.classList.add('btn') if (answer.correct) { button.dataset.correct = answer.correct } button.addEventListener('click', selectAnswer) answerButtonsElement.appendChild(button) }) } function resetState() { clearStatusClass(document.body) nextButton.classList.add('hide') while (answerButtonsElement.firstChild) { answerButtonsElement.removeChild(answerButtonsElement.firstChild) } trybutton.classList.add('hide') } function selectAnswer(e) { const selectedButton = e.target const correct = selectedButton.dataset.correct setStatusClass(document.body, correct) setStatusClass(selectedButton, correct); if(correct){ if (shuffledQuestions.length > currentQuestionIndex + 1) { nextButton.classList.remove('hide') } else { endbutton.classList.remove('hide') } } else{ trybutton.classList.remove('hide') } } function setStatusClass(element, correct) { clearStatusClass(element) if (correct) { element.classList.add('correct') } else { element.classList.add('wrong') } } function clearStatusClass(element) { element.classList.remove('correct') element.classList.remove('wrong') } const questions = [ { question: 'What is 1+2?', answers: [ { text: '3', correct: true }, { text: '4', correct: false }, { text: '5', correct: false }, { text: '6', correct: false } ] }, { question: 'What is 2-2?', answers: [ { text: '0', correct: true }, { text: '4', correct: false }, { text: '5', correct: false }, { text: '7', correct: false }, ] }, ]
 *, *::before, *::after { box-sizing: border-box; font-family: cursive, 'Times New Roman', Times, serif } #particles-js { position: absolute; width: 100%; height: 100%; background-repeat: no-repeat; background-size: cover; background-position: 50% 50%; z-index: 1; }:root { --hue-neutral: 200; --hue-wrong: 0; --hue-correct: 145; } body { --hue: var(--hue-neutral); padding: 0; margin: 0; display: flex; width: 100vw; height: 100vh; justify-content: center; align-items: center; background-color: hsl(var(--hue), 100%, 20%); } body.correct { --hue: var(--hue-correct); } body.wrong { --hue: 0; }.container { width: 800px; max-width: 80%; background-color: white; border-radius: 5px; padding: 10px; box-shadow: 0 0 10px 2px; z-index: 2; }.btn-grid { display: grid; grid-template-columns: repeat(2, auto); gap: 10px; margin: 20px 0; }.btn { --hue: var(--hue-neutral); border: 1px solid hsl(var(--hue), 100%, 30%); background-color: hsl(var(--hue), 100%, 50%); border-radius: 5px; padding: 5px 10px; color: white; outline: none; }.btn:hover { border-color: black; }.btn.correct { --hue: var(--hue-correct); color: black; }.btn.wrong { --hue: var(--hue-wrong); }.next-btn { font-size: 1.5rem; font-weight: bold; padding: 10px 20px; align-items: flex-end; --hue: 245; }.start-btn { font-size: 1.5rem; font-weight: bold; padding: 10px 20px; --hue: 245; }.end-btn { font-size: 1.5rem; font-weight: bold; padding: 10px 20px; --hue: 245; }.try-btn { font-size: 1.5rem; font-weight: bold; padding: 10px 20px; --hue: 245; }.container1 { display: flex; justify-content: center; align-items: center; font-family: Arial; font-size: xx-large; padding: 10px 10px; }.container2 { width: 800px; max-width: 80%; background-color: white; border-radius: 5px; padding: 10px; box-shadow: 0 0 10px 2px; top:37%; position:absolute; display: flex; }.controls { display: flex; justify-content: center; align-items: center; }.hide { display: none; }.wrapper { position: absolute; top: 0px; right: 0px; }
 <,DOCTYPE html> <html lang="en"> <.DOCTYPE html> <html lang="en"> <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>

暂无
暂无

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

相关问题 单击交换按钮时,如何使红色div更改背景颜色以在红色和绿色之间切换? - How do I get my red div to change background color to toggle between red and green when I click on the swap button? 如何在不使用内联CSS的情况下使用彩色文本框(红色/绿色)进行Javascript表单验证? - How to make a Javascript form validation with coloured textbox (red/green) without using inline css? 我想改变一个的颜色 <p> 只需单击一个按钮,即可将标签从红色标记为绿色 - I want to change colour of a <p> tag from red to green on clicking only one button 如何为此JavaScript / HTML代码添加一个按钮,以便我的代码仅在按下按钮后才会运行? - How do i add a button to this JavaScript/HTML code, so that my code only runs once the button is pressed? 如何控制我的 canvas 的 RGB,使渐变的红色和绿色部分是我鼠标 position 的 position 除以二? - How can I control the RGB of my canvas so that the Red and Green part of the gradient is the position of my mouse position divided by two? 如何让我的文本从红色 div 环绕到绿色 div? - How do I get my text to wrap from the red div to the green div? 如何使用 CSS 修复红色和绿色块? - How do I fix red and green blocks with CSS ? 如何使用表格中的单选按钮更改背景颜色。 每个单选按钮4种颜色(白色,红色,黄色,绿色) - How do i change background color using radio buttons in tables. 4 colors(white, red, yellow, green) to each radio buttons 我需要更改字体颜色。 如果 passed = green and Failed = red 但它在表中(循环语句)所以我不知道 - I need to change font color. If passed = green and Failed = red but it's in the table(Loop Statement) so I don't know 将测验中所有单选按钮的标签文本更改为红色和绿色 - change label text of all radio button in quiz as red and green
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM