简体   繁体   English

如何检查具有可变子元素的元素是否包含具有特定值的数组属性?

[英]How can I check if an element with a changeable child element contains an array property with a certain value?

Can someone please have a look at my code and let me know how I can write this function.有人可以看看我的代码,让我知道如何编写这个函数。 I am creating a sequence quiz where the answers have to be placed in the correct order.我正在创建一个序列测验,答案必须按正确的顺序排列。

When The player is happy with their order there is a check answers function.当玩家对他们的订单感到满意时,就会有一个检查答案功能。 The questions come from an array where a {"correct_order" :1} property is defined for each answer.问题来自一个数组,其中为每个答案定义了一个{"correct_order" :1}属性。

I'm wondering how I can now write the code to check if {"correct_order" :1} is contained within the answerOneContainer.我想知道我现在如何编写代码来检查{"correct_order" :1}是否包含在 answerOneContainer 中。 Below is the HTML for the quiz container and complete JS and my attempt to check if the container contains {"correct_order" :1} inside the // CHECK ANSWER FUNCTION but I am unsure how to write this piece of code:下面是测验容器和完整 JS 的 HTML 以及我尝试检查容器是否在 // CHECK ANSWER FUNCTION 中包含{"correct_order" :1}但我不确定如何编写这段代码:

 // VARIABLES const quizQuestion = document.getElementById('question'); const answerOne = document.getElementById('ans1'); const answerTwo = document.getElementById('ans2'); const answerThree = document.getElementById('ans3'); const answerFour = document.getElementById('ans4'); const answerFive = document.getElementById('ans5'); const answerOneContainer = document.getElementById('container1'); const answerTwoContainer = document.getElementById('container2'); const answerThreeContainer = document.getElementById('container3'); const answerFourContainer = document.getElementById('container4'); const answerFiveContainer = document.getElementById('container5'); // QUIZ QUESTIONS ARRAY const myQuestions = [ { question: "Starting with the most put these countries in order of total population", answers: [ { "correct_order": 5, "details": "Ireland" }, { "correct_order": 2, "details": "United States" }, { "correct_order": 3, "details": "Russia" }, { "correct_order": 1, "details": "China" }, { "correct_order": 4, "details": "United Kingdom" } ] }, { question: "Starting with the most put these countries in order of size", answers: [ { "correct_order": 2, "details": "Canada" }, { "correct_order": 5, "details": "Ireland" }, { "correct_order": 3, "details": "Australia" }, { "correct_order": 4, "details": "Mexico" }, { "correct_order": 1, "details": "Russia" } ] }, { question: "Starting with the most put these languages in order of most spoken", answers: [ { "correct_order": 2, "details": "English" }, { "correct_order": 5, "details": "Arabic" }, { "correct_order": 3, "details": "Hindustani" }, { "correct_order": 4, "details": "Spanish" }, { "correct_order": 1, "details": "Mandarin Chinese" } ] } ] // BUILD QUIZ FUNCTION document.getElementById("start-quiz").onclick = function () { buildQuiz() }; let counter = 0; function buildQuiz() { document.getElementById("callout").classList.add("d-none"); document.getElementById("quiz").classList.remove("d-none"); quizQuestion.innerHTML = myQuestions[counter].question; answerOne.innerHTML = myQuestions[counter].answers[0].details; answerTwo.innerHTML = myQuestions[counter].answers[1].details; answerThree.innerHTML = myQuestions[counter].answers[2].details; answerFour.innerHTML = myQuestions[counter].answers[3].details; answerFive.innerHTML = myQuestions[counter].answers[4].details; }; // SWAP ANSWERS FUNCTION let answerContainers = { "container1": answerOneContainer, "container2": answerTwoContainer, "container3": answerThreeContainer, "container4": answerFourContainer, "container5": answerFiveContainer, } function swapAnswersContainer(switchButton, container1, container2) { document.getElementById(switchButton).addEventListener('click', () => { $("#" + container1).children(":first").slideToggle('fast'); $("#" + container2).children(":first").slideToggle('fast', function () { answerContainers[container2].appendChild(answerContainers[container1].firstElementChild); answerContainers[container1].appendChild(answerContainers[container2].firstElementChild); }); $("#" + container1).children(":first").slideToggle('fast'); $("#" + container2).children(":first").slideToggle('fast'); }); } swapAnswersContainer("switch1", "container1", "container2"); swapAnswersContainer("switch2", "container2", "container3"); swapAnswersContainer("switch3", "container3", "container4"); swapAnswersContainer("switch4", "container4", "container5"); // CHECK ANSWER FUNCTION document.getElementById("quizSubmit").onclick = function () { checkAnswer() }; function checkAnswer() { document.getElementById("quizSubmit").classList.add("d-none"); document.getElementById("nextQuestion").classList.remove("d-none"); let score = 0; if (answerOneContainer.firstElementChild === "correct_order": "1") { document.getElementById("container1").classList.add("green-border") } else { document.getElementById("container1").classList.add("red-border") } }; // NEXT QUESTION FUNCTION document.getElementById("nextQuestion").onclick = function () { nextButton() }; function nextButton() { if (counter >= myQuestions.length) { counter = 0; return; } counter++; buildQuiz(); document.getElementById("nextQuestion").classList.add("d-none"); document.getElementById("quizSubmit").classList.remove("d-none"); };
 <div id="quiz" class="quiz-container d-none"> <div id="question" class="quiz-question"></div> <div id="container1" class="answer-container"> <div id="ans1" class="answer"></div> </div> <div class="button-container"> <button id="switch1" class="btn-style"><i class="fas fa-sync switch-button"></i></button> </div> <div id="container2" class="answer-container"> <div id="ans2" class="answer"></div> </div> <div class="button-container"> <button id="switch2" class="btn-style"><i class="fas fa-sync switch-button"></i></button> </div> <div id="container3" class="answer-container"> <div id="ans3" class="answer"></div> </div> <div class="button-container"> <button id="switch3" class="btn-style"><i class="fas fa-sync switch-button"></i></button> </div> <div id="container4" class="answer-container"> <div id="ans4" class="answer"></div> </div> <div class="button-container"> <button id="switch4" class="btn-style"><i class="fas fa-sync switch-button"></i></button> </div> <div id="container5" class="answer-container"> <div id="ans5" class="answer"></div> </div> <button id="quizSubmit" class="quiz-submit btn btn-primary btn-lg">Check Answer</button> <button id="nextQuestion" class="quiz-submit btn btn-primary btn-lg d-none">Next Question</button> </div>

You have a lot of errors in your code, it's pretty difficult to decipher exactly what you want to do.您的代码中有很多错误,很难准确解读您想要做什么。

So far I made these changes to try to give you a minimal reproducible example ( https://stackoverflow.com/help/minimal-reproducible-example )到目前为止,我进行了这些更改以尝试为您提供一个最小的可重现示例( https://stackoverflow.com/help/minimal-reproducible-example

You need to post some code that we can ourselves run and test ( https://stackoverflow.com/help/minimal-reproducible-example ), please try to edit the answer with the "code snippet" option.您需要发布一些我们可以自己运行和测试的代码( https://stackoverflow.com/help/minimal-reproducible-example ),请尝试使用“代码片段”选项编辑答案。

 // VARIABLES const quizQuestion = document.getElementById('question'); const answerOne = document.getElementById('ans1'); const answerTwo = document.getElementById('ans2'); const answerThree = document.getElementById('ans3'); const answerFour = document.getElementById('ans4'); const answerFive = document.getElementById('ans5'); const answerOneContainer = document.getElementById('container1'); const answerTwoContainer = document.getElementById('container2'); const answerThreeContainer = document.getElementById('container3'); const answerFourContainer = document.getElementById('container4'); const answerFiveContainer = document.getElementById('container5'); // QUIZ QUESTIONS ARRAY const myQuestions = [ { question: "Starting with the most put these countries in order of total population", answers: [ { "correct_order": 5, "details": "Ireland" }, { "correct_order": 2, "details": "United States" }, { "correct_order": 3, "details": "Russia" }, { "correct_order": 1, "details": "China" }, { "correct_order": 4, "details": "United Kingdom" } ] }, { question: "Starting with the most put these countries in order of size", answers: [ { "correct_order": 2, "details": "Canada" }, { "correct_order": 5, "details": "Ireland" }, { "correct_order": 3, "details": "Australia" }, { "correct_order": 4, "details": "Mexico" }, { "correct_order": 1, "details": "Russia" } ] }, { question: "Starting with the most put these languages in order of most spoken", answers: [ { "correct_order": 2, "details": "English" }, { "correct_order": 5, "details": "Arabic" }, { "correct_order": 3, "details": "Hindustani" }, { "correct_order": 4, "details": "Spanish" }, { "correct_order": 1, "details": "Mandarin Chinese" } ] } ] // BUILD QUIZ FUNCTION document.getElementById("start-quiz").onclick = buildQuiz; let counter = 0; function buildQuiz() { //document.getElementById("callout").classList.add("d-none"); document.getElementById("quiz").classList.remove("d-none"); quizQuestion.innerHTML = myQuestions[counter].question; answerOne.innerHTML = myQuestions[counter].answers[0].details; answerTwo.innerHTML = myQuestions[counter].answers[1].details; answerThree.innerHTML = myQuestions[counter].answers[2].details; answerFour.innerHTML = myQuestions[counter].answers[3].details; answerFive.innerHTML = myQuestions[counter].answers[4].details; }; // SWAP ANSWERS FUNCTION let answerContainers = { container1: answerOneContainer, container2: answerTwoContainer, container3: answerThreeContainer, container4: answerFourContainer, container5: answerFiveContainer } function swapAnswersContainer(switchButton, container1, container2) { document.getElementById(switchButton).addEventListener('click', () => { $("#" + container1).children(":first").slideToggle('fast'); $("#" + container2).children(":first").slideToggle('fast', function () { answerContainers[container2].appendChild(answerContainers[container1].firstElementChild); answerContainers[container1].appendChild(answerContainers[container2].firstElementChild); }); $("#" + container1).children(":first").slideToggle('fast'); $("#" + container2).children(":first").slideToggle('fast'); }); } swapAnswersContainer("switch1", "container1", "container2"); swapAnswersContainer("switch2", "container2", "container3"); swapAnswersContainer("switch3", "container3", "container4"); swapAnswersContainer("switch4", "container4", "container5"); // CHECK ANSWER FUNCTION document.getElementById("quizSubmit").onclick = checkAnswer; function checkAnswer() { document.getElementById("quizSubmit").classList.add("d-none"); document.getElementById("nextQuestion").classList.remove("d-none"); let score = 0; if (answerOneContainer.firstElementChild["correct_order"] === "1") { document.getElementById("container1").classList.add("green-border") } else { document.getElementById("container1").classList.add("red-border") } }; // NEXT QUESTION FUNCTION document.getElementById("nextQuestion").onclick = nextButton; function nextButton() { if (counter >= myQuestions.length) { counter = 0; return; } counter++; buildQuiz(); document.getElementById("nextQuestion").classList.add("d-none"); document.getElementById("quizSubmit").classList.remove("d-none"); };
 <div id="quiz" class="quiz-container d-none"> <div id="question" class="quiz-question"></div> <div id="container1" class="answer-container"> <div id="ans1" class="answer"></div> </div> <div class="button-container"> <button id="switch1" class="btn-style"><i class="fas fa-sync switch-button"></i></button> </div> <div id="container2" class="answer-container"> <div id="ans2" class="answer"></div> </div> <div class="button-container"> <button id="switch2" class="btn-style"><i class="fas fa-sync switch-button"></i></button> </div> <div id="container3" class="answer-container"> <div id="ans3" class="answer"></div> </div> <div class="button-container"> <button id="switch3" class="btn-style"><i class="fas fa-sync switch-button"></i></button> </div> <div id="container4" class="answer-container"> <div id="ans4" class="answer"></div> </div> <div class="button-container"> <button id="switch4" class="btn-style"><i class="fas fa-sync switch-button"></i></button> </div> <div id="container5" class="answer-container"> <div id="ans5" class="answer"></div> </div> <button id="quizSubmit" class="quiz-submit btn btn-primary btn-lg">Check Answer</button> <button id="nextQuestion" class="quiz-submit btn btn-primary btn-lg d-none">Next Question</button> <button id="start-quiz" class="quiz-submit btn btn-primary btn-lg d-none">Start Quiz</button> </div>

暂无
暂无

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

相关问题 如何检查具有可变子元素的元素是否包含具有特定值的数组属性? - How can I check if an element with a changeable child contains an array property with a certain value? 如何使用 Chai.js 检查数组中的任何元素是否包含大于 0 的值? - How can I check if any element in an array contains a value above 0 using Chai.js? 如何检查字符串是否包含数组中的某个元素,但不包含其他元素 - How to check if a string contains a certain element from an array, but not the other elements 检查数组元素的一部分是否包含某些字符串 - check if a part of an array element contains certain string 如何使用Chai检查从API提取的数组中元素的属性是否等于某个值? - How to check if the property of an element in an array fetched from the API is equal to certain value using Chai? 如何为数组中的第一个元素返回属性值,如果没有元素则返回0? - How can I return a property value for the first element in an array or 0 if there are no elements? 检查元素是否具有某个 class 并且属于包含某个值的表单 - Check if element has a certain class and belongs to a form that contains a certain value 如何选择具有特定属性的元素的子级 - how to select a child of an element with a certain property Angular.js:如何检查元素的子元素是否包含某些元素(按ID) - Angularjs: How to check if element's children contains certain element (by ID) 如何检查单击的元素是否包含具有特定类的元素 - How to check if clicked element contains element with certain class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM