我想做两级手风琴菜单.第一级很好,但当我添加第二级时,第二级内容隐藏在第一级第2节标题中.有人知道我该怎么解决这个问题?

我的html代码如下:

<div class="container-wrapper"></div>
    <button class="accordion"><p class="title-sections">Provisioning</p></button>
    <div class="panel">
        <button class="sub-accordion">Set Variables</button>
        <div class="sub-panel">
            <p>Lorem ipsum...</p>
        </div>
        <button class="sub-accordion">Get Variables</button>
        <div class="sub-panel">
            <p>Lorem ipsum...</p>
         </div>
         <button class="sub-accordion">Reset</button>
         <div class="sub-panel">
             <p>Lorem ipsum...</p>
         </div>
         <button class="sub-accordion">Get Base Report</button>
         <div class="sub-panel">
             <p>Lorem ipsum...</p>
         </div>
         </div>
    
     <button class="accordion"><p class="title-sections">Availability</p></button>
     <div class="panel">
         <p>Lorem ipsum...</p>
     </div>

我的CSS:

.accordion {
        background-color: #eee;
        color: #444;
        cursor: pointer;
        padding: 18px;
        width: 100%;
        text-align: left;
        border: none;
        outline: none;
        transition: 0.4s;
    }

    .sub-accordion {
        background-color: rgb(181, 255, 191);
        color: #444;
        cursor: pointer;
        padding: 18px;
        width: 100%;
        text-align: center;
        border: none;
        outline: none;
        transition: 0.4s;
    }

    .active, .accordion:hover {
       background-color: #ccc;
    }

    .sub-active, .sub-accordion:hover {
        background-color: rgb(104, 255, 124);;
    }

    .panel {
      padding: 0 18px;
      background-color: white;
      max-height: 0;
      overflow: hidden;
      transition: max-height 0.2s ease-out;
    }

    .sub-panel {
        padding: 0 18px;
        background-color: lightblue;
        max-height: 0;
        overflow: hidden;
        transform: max-height 0.2s ease-out;
    }

    .container-wrapper {
        padding: 20pt;
    }

    .title-sections {
        font-size: 20px;
    }

最后,我的JS,问题来了.我复制粘贴了第一级手风琴的相同代码来制作第二级手风琴,但高度有问题,但我不太知道需要更改哪个部分:

var acc = document.getElementsByClassName("accordion");
var i;
var sub_acc = document.getElementsByClassName("sub-accordion");
var j;


for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    }
  });
}

for (j = 0; j < sub_acc.length; j++) {
  sub_acc[j].addEventListener("click", function() {
    this.classList.toggle("sub-active");
    var sub_panel = this.nextElementSibling;
    if (sub_panel.style.maxHeight) {
      sub_panel.style.maxHeight = null;
    } else {
      sub_panel.style.maxHeight = sub_panel.scrollHeight + "px";
    }
  });
}

推荐答案

下面是Javascript中的修复程序.解决方案只是寻找活动的手风琴,并在sub accordion click logic中再次设置其最大高度

const activeAccordion = document.getElementsByClassName("accordion active")[0]
var panel = activeAccordion.nextElementSibling;
panel.style.maxHeight = panel.scrollHeight + "px";

与Javascript完全集成

var acc = document.getElementsByClassName("accordion");
var i;
var sub_acc = document.getElementsByClassName("sub-accordion");
var j;


for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    }
  });
}

for (j = 0; j < sub_acc.length; j++) {
  sub_acc[j].addEventListener("click", function() {
    this.classList.toggle("sub-active");
    var sub_panel = this.nextElementSibling;
    if (sub_panel.style.maxHeight) {
      sub_panel.style.maxHeight = null;
    } else {
      sub_panel.style.maxHeight = sub_panel.scrollHeight + "px";
    }

    //The change is here
    const activeAccordion = document.getElementsByClassName("accordion active")[0]
    var panel = activeAccordion.nextElementSibling;
    panel.style.maxHeight = panel.scrollHeight + "px";
  });
}
.accordion {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  text-align: left;
  border: none;
  outline: none;
  transition: 0.4s;
}

.sub-accordion {
  background-color: rgb(181, 255, 191);
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  text-align: center;
  border: none;
  outline: none;
  transition: 0.4s;
}

.active,
.accordion:hover {
  background-color: #ccc;
}

.sub-active,
.sub-accordion:hover {
  background-color: rgb(104, 255, 124);
  ;
}

.panel {
  padding: 0 18px;
  background-color: white;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}

.sub-panel {
  padding: 0 18px;
  background-color: lightblue;
  max-height: 0;
  overflow: hidden;
  transform: max-height 0.2s ease-out;
}

.container-wrapper {
  padding: 20pt;
}

.title-sections {
  font-size: 20px;
}
<div class="container-wrapper"></div>
<button class="accordion"><p class="title-sections">Provisioning</p></button>
<div class="panel">
  <button class="sub-accordion">Set Variables</button>
  <div class="sub-panel">
    <p>Lorem ipsum...</p>
  </div>
  <button class="sub-accordion">Get Variables</button>
  <div class="sub-panel">
    <p>Lorem ipsum...</p>
  </div>
  <button class="sub-accordion">Reset</button>
  <div class="sub-panel">
    <p>Lorem ipsum...</p>
  </div>
  <button class="sub-accordion">Get Base Report</button>
  <div class="sub-panel">
    <p>Lorem ipsum...</p>
  </div>
</div>

<button class="accordion"><p class="title-sections">Availability</p></button>
<div class="panel">
  <p>Lorem ipsum...</p>
</div>

Javascript相关问答推荐

如何指定1条记录1个表?

脚本.js:3:20未捕获的类型错误:无法读取空的属性(读取addEventHandler)

传递一个大对象以在Express布局中呈现

被CSS优先级所迷惑

如何在Connect 4游戏中 for each 玩家使用位板寻找7形状?

浮动Div的淡出模糊效果

为什么Mutations 观察器用微任务队列而不是macrotask队列处理?

我创建了一个创建对象的函数,我希望从该函数创建的对象具有唯一的键.我怎么能做到这一点?

禁用.js文件扩展名并从目录导入隐式根index.js时,找不到NodeJS导入模块

为什么我的自定义元素没有被垃圾回收?

根据一个条件,如何从处理过的数组中移除一项并将其移动到另一个数组?

如何使用JS创建一个明暗功能按钮?

如何在 Select 文本时停止Click事件?

自定义确认组件未在vue.js的v菜单内打开

按下单键和多值

如何使本地html页面在重新加载时保持当前可隐藏部分的打开状态?

ngOnChanges仅在第二次调用时才触发

使用可配置项目创建网格

将Singleton实例设置为未定义后的Angular 变量引用持久性行为

AstroJS混合模式服务器终结点返回404