[英]How can I collapse all tabs in a vertical accordion with Javascript
[英]How do I collapse all on load with a nested JavaScript accordion
我用JavaScript創建了一個嵌套的手風琴。 在加載時,兩個父手風琴是關閉的,但是嵌套的手風琴都是打開的,箭頭指向錯誤的方向。 如何使所有嵌套的手風琴也關閉?
這是代碼: https : //jsfiddle.net/mike4323/spfqf1t5/
這是手風琴代碼
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active");
/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
}
}
在您的html中,嵌套手風琴<button>
的<div>
兄弟姐妹沒有類panel
用於類的css panel
display: none;
基本上,將類panel
添加到嵌套手風琴<button>
的<div>
兄弟姐妹中,或為這些div
sa display: none;
默認。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.