简体   繁体   English

如何使用嵌套JavaScript手风琴折叠所有负载

[英]How do I collapse all on load with a nested JavaScript accordion

I have created a nested accordion with JavaScript. 我用JavaScript创建了一个嵌套的手风琴。 on load the two parent accordions are closed but the nested accordions are all open with the arrow pointing the wrong way. 在加载时,两个父手风琴是关闭的,但是嵌套的手风琴都是打开的,箭头指向错误的方向。 How do i make it so all nested accordions are also closed? 如何使所有嵌套的手风琴也关闭?

here is the code: https://jsfiddle.net/mike4323/spfqf1t5/ 这是代码: https : //jsfiddle.net/mike4323/spfqf1t5/

This is the accordion code 这是手风琴代码

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";
    }
  }
}

in your html, the <div> siblings of the nested accordion <button> do not have the class panel 在您的html中,嵌套手风琴<button><div>兄弟姐妹没有类panel

The css for class panel has display: none; 用于类的css panel display: none;

basically, add the class panel to the <div> siblings of the nested accordion <button> or give those div sa display: none; 基本上,将类panel添加到嵌套手风琴<button><div>兄弟姐妹中,或为这些div sa display: none; by default. 默认。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM