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

时间:2017-04-07 23:26:56

标签: 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";
    }
  }
}

1 个答案:

答案 0 :(得分:0)

在您的html中,嵌套手风琴<div>的{​​{1}}兄弟姐妹没有班级<button>

班级panel的css有panel

基本上,将课程display: none;添加到嵌套手风琴panel的{​​{1}}兄弟姐妹中,或默认情况下提供<div> sa <button>