[英]How to get FAQ on a website working without using classnames?
I'm trying to fix this accordion on a FAQ page.我正在尝试在常见问题页面上修复这个手风琴。 The question should open up the p tag with the answer when you click the h3 tag.
当您单击 h3 标签时,问题应该打开带有答案的 p 标签。 Unfortunately it is not possible to use any class name or anything like that.
不幸的是,不可能使用任何类名或类似的名称。 Our client can now simply add a h3 and ap tag to create a new question.
我们的客户现在可以简单地添加一个 h3 和 ap 标签来创建一个新问题。 I'm trying to open up one question a the time.
我试图一次打开一个问题。 Somebody who can help me with this?
谁能帮我解决这个问题?
<style>
.faq h3{ background: #e2001a; color: #fff; padding: 5px 10px; cursor:
pointer; user-select: none;}
.show {
display: block;
}
</style>
<div class="faq">
<h1>FAQ</h1>
<h3>Question 1</h3>
<p>Lorem ipsum</p>
<h3>Question 2</h3>
<p>Lorem ipsum</p>
<h3>Question 3</h3>
<p>Lorem ipsum</p>
<script>
var acc = document.getElementsByClassName(".faq h3");
var panel = document.getElementsByClassName(".faq p");
for (var i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
var setClasses = !this.classList.contains('active');
setClass(acc, 'active', 'remove');
setClass(panel, 'show', 'remove');
if (setClasses) {
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
}
function setClass(els, className, fnName) {
for (var i = 0; i < els.length; i++) {
els[i].classList[fnName](className);
}
}
</script>
Use nextElementSibling
to target the paragraph of each clicked h3
.使用
nextElementSibling
定位每个点击的h3
的段落。
// Grab all the h3s and paras const h3s = document.querySelectorAll('h3'); const paras = document.querySelectorAll('p'); // Add click listeners to each h3 h3s.forEach(h3 => h3.addEventListener('click', handleClick, false)); function handleClick() { // Grab the nextElementSibling from the clicked element const { nextElementSibling: next } = this; // Remove all `show` classes from the paras paras.forEach(para => para.classList.remove('show')); // Add a show class to the sibling para next.classList.toggle('show'); }
p { visibility: hidden; height: 0px; } .show { visibility: visible; height: auto; }
<div class="faq"> <h1>FAQ</h1> <h3>Question 1</h3> <p>Lorem ipsum</p> <h3>Question 2</h3> <p>Lorem ipsum</p> <h3>Question 3</h3> <p>Lorem ipsum</p> </div>
There are some small mistakes in your selectors.您的选择器中有一些小错误。 In getElementsByClassName('name') the name has to be a simple string (the class name ), but you can look for child nodes with getElementsByTagName('name') - for example.
在getElementsByClassName('name') 中,名称必须是一个简单的字符串(类名),但您可以使用getElementsByTagName('name')查找子节点 - 例如。
If you want to use CSS selectors use querySelectorAll instead:如果要使用 CSS 选择器,请改用querySelectorAll :
var acc = document.querySelectorAll("div.faq > h3");
var panel = document.querySelectorAll('div.faq > p');
This example is using getElementsByClassName and getElementsByTagName:此示例使用 getElementsByClassName 和 getElementsByTagName:
var faq = document.getElementsByClassName("faq")[0]; var acc = faq.getElementsByTagName('h3'); var panel = faq.getElementsByTagName('p'); for (var i = 0; i < acc.length; i++) { acc[i].onclick = function() { var setClasses = !this.classList.contains("active"); setClass(acc, "active", "remove"); setClass(panel, "show", "remove"); if (setClasses) { this.classList.toggle("active"); this.nextElementSibling.classList.toggle("show"); } }; } function setClass(els, className, fnName) { for (var i = 0; i < els.length; i++) { els[i].classList[fnName](className); } }
.faq h3 { background: #e2001a; color: #fff; padding: 5px 10px; cursor: pointer; user-select: none; } .faq p { display: none; } .faq p.show { display: block; }
<div class="faq"> <h1>FAQ</h1> <h3>Question 1</h3> <p>Lorem ipsum</p> <h3>Question 2</h3> <p>Lorem ipsum</p> <h3>Question 3</h3> <p>Lorem ipsum</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.