'Accordion For Loop JS Selector”改写为中文可以是“手风琴循环JS选择器”。示例代码如下:
HTML:
标签1
内容1
标签2
内容2
标签3
内容3
CSS:
.accordion-content {
display: none;
}
JS:
const headers = document.querySelectorAll(".accordion-header");
for (let i = 0; i < headers.length; i++) {
headers[i].addEventListener("click", function() {
// 收起所有内容
const contents = document.querySelectorAll(".accordion-content");
for (let j = 0; j < contents.length; j++) {
contents[j].style.display = "none";
}
// 展开当前内容
this.nextElementSibling.style.display = "block";
});
}