[英]How to target a Class inside div (automatically on click)
我想获取所有带有课堂questions
元素,给它们一个 onclick,然后针对特定的点击元素。 在这个点击的元素中,我想获取带有类answer-text
的 div 并添加活动类。
我现在的代码:
var getQuestions = document.querySelectorAll('.questions'); getQuestions.forEach(function(question) { question.addEventListener('click', function(e) { var target = e.target; var answer = target.getElementsByClassName('answer-text'); answer.classList.add('active'); }) });
.answer-text { display: none; } .active { display: block; } .questions { padding: 20px; font-weight: 700; }
<div class="questions"> <span class="icon icon-vu-arrow-top-stroke"></span><div class="question-text"> Wat mag/kan wel en wat niet tijdens mijn uitje of vakantie? </div> <div class="answer-text"> Antwoord 6 </div> </div> <div class="questions"> <span class="icon icon-vu-arrow-top-stroke"></span><div class="question-text"> Kan ik mijn uitje wijzigen? </div> <div class="answer-text"> Antwoord 7 </div> </div>
我认为/知道我真的很接近它的工作,但无法完成使其工作的最后一步。 有谁知道如何解决这个问题?
代表:
document.getElementById('questions').addEventListener("click",function(e) { const tgt = e.target; if (tgt.classList.contains("question-text")) { tgt.closest(".questions").querySelector('.answer-text').classList.add('active'); } });
.answer-text { display: none; } .active { display: block; } .questions { padding: 20px; font-weight: 700; }
<div id="questions"> <div class="questions"> <span class="icon icon-vu-arrow-top-stroke"></span> <div class="question-text"> Wat mag/kan wel en wat niet tijdens mijn uitje of vakantie? </div> <div class="answer-text"> Antwoord 6 </div> </div> <div class="questions"> <span class="icon icon-vu-arrow-top-stroke"></span> <div class="question-text"> Kan ik mijn uitje wijzigen? </div> <div class="answer-text"> Antwoord 7 </div> </div> </div>
阅读代码中的注释以进行解释
var getQuestions = document.querySelectorAll('.questions'); getQuestions.forEach(function(question) { question.addEventListener('click', function(e) { var target = e.target; // change the line: // var answer = target.getElementsByClassName('answer-text'); // to the following: var answer = this.closest('.questions').querySelector('.answer-text'); // what happens is: it goes up the DOM to the closest question // from where the click happened and looks inside the .questions for the .answer-text element answer.classList.add('active'); }) });
.answer-text { display: none; } .active { display: block; } .questions { padding: 20px; font-weight: 700; }
<div class="questions"> <span class="icon icon-vu-arrow-top-stroke"></span><div class="question-text"> Wat mag/kan wel en wat niet tijdens mijn uitje of vakantie? </div> <div class="answer-text"> Antwoord 6 </div> </div> <div class="questions"> <span class="icon icon-vu-arrow-top-stroke"></span><div class="question-text"> Kan ik mijn uitje wijzigen? </div> <div class="answer-text"> Antwoord 7 </div> </div>
检查您的html
文件, answer-text
属于.questions
元素而不是.question-text
元素。
因此,在每个.question-text
单击事件处理程序上,要获得所选问题的答案,您需要获取所选.question-text
的parentElement
并在该父元素上找到answer
。
并且getElementsByClassName
返回HTMLCollection
所以answer[0]
将返回目标答案元素。
var getQuestions = document.querySelectorAll('.questions'); getQuestions.forEach(function(question) { question.addEventListener('click', function(e) { var target = e.target; var answer = target.parentElement.getElementsByClassName('answer-text'); answer[0].classList.add('active'); }) });
.answer-text { display: none; } .active { display: block; } .questions { padding: 20px; font-weight: 700; }
<div class="questions"> <span class="icon icon-vu-arrow-top-stroke"></span> <div class="question-text"> Wat mag/kan wel en wat niet tijdens mijn uitje of vakantie? </div> <div class="answer-text"> Antwoord 6 </div> </div> <div class="questions"> <span class="icon icon-vu-arrow-top-stroke"></span> <div class="question-text"> Kan ik mijn uitje wijzigen? </div> <div class="answer-text"> Antwoord 7 </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.