包含多个类的JavaScript Accordion脚本

时间:2018-06-14 14:07:08

标签: javascript jquery html css accordion

我不是JavaScript程序员,但我需要用它来编辑我的组织的网站。

该页面由具有嵌套手风琴的父手风琴和嵌套手风琴中列出的项目组成。

我希望能够以与嵌套手风琴不同的方式设定父母手风琴的风格,以区分两者;因此,例如,嵌套手风琴的颜色与父母手风琴的颜色不同。

代码在很大程度上起作用,但是我遇到一个问题,即一个父手风琴中的内容没有正确地与其他父手风琴的内容形式进行交互。

第一个问题: 当我第一次加载页面然后展开顶级父手风琴,然后展开嵌套的手风琴时,最下面列出的项目将落在下一个父手风琴后面并被遮挡。如果我然后收缩嵌套的手风琴然后再展开它们,那么列出的项目就会与下面的父母手风琴正确互动。

第二个问题: 第二个问题是如何正确地设计两种手风琴的风格。我尝试了几种不同的方法来解决这个问题。一种方法是创建一个调用:

的辅助脚本
var acc = document.getElementsByClassName("nest");

然后给嵌套的手风琴提供类名“nest。”

另一个是将我的CSS链接到我的嵌套手风琴。使用 - button.accordion - 用于父手风琴然后 - div.panel button.accordion - 用于嵌套手风琴。

有关如何正确解决这两个问题的任何指导?这是我第一次发布到stackoverflow,所以如果我在帖子中做了任何失礼,我会道歉。

使用的手风琴脚本直接来自w3schools.com

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var parent = this.parentElement;
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
      parent.style.maxHeight = parseInt(parent.style.maxHeight) +
        panel.scrollHeight + "px";
    }
  });
}
<!-- first accordion section -->
<!-- first parent accordion -->
<button class="accordion">Name of Parent Accordion</button>
<div class="panel">
  <!-- first nested accordion -->
  <button class="accordion">Name of Nested Accordion</button>
  <div class="panel">
    <ul>
      <li>List Item</li>
      <li>List Item</li>
      <li>List Item</li>
    </ul>
  </div>
  <!-- second nested accordion -->
  <button class="accordion">Name of Nested Accordion</button>
  <div class="panel">
    <ul>
      <li>List Item</li>
      <li>List Item</li>
      <li>List Item</li>
    </ul>
  </div>
</div>
<!-- second accordion section -->
<!-- second parent accordion -->
<button class="accordion">Name of Parent Accordion</button>
<div class="panel">
  <!-- first nested accordion -->
  <button class="accordion">Name of Nested Accordion</button>
  <div class="panel">
    <ul>
      <li>List Item</li>
      <li>List Item</li>
      <li>List Item</li>
    </ul>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
    transition: 0.4s;
}

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

.panel {
    padding: 0 18px;
    display: none;
    background-color: white;
    overflow: hidden;
}
</style>
</head>
<body>

<h2>Accordion</h2>



<!-- first accordion section -->
<!-- first parent accordion -->
<button class="accordion">Name of Parent Accordion</button>
<div class="panel">
  <!-- first nested accordion -->
  <button class="accordion">Name of Nested Accordion</button>
  <div class="panel">
    <ul>
      <li>List Item</li>
      <li>List Item</li>
      <li>List Item</li>
    </ul>
  </div>
  <!-- second nested accordion -->
  <button class="accordion">Name of Nested Accordion</button>
  <div class="panel">
    <ul>
      <li>List Item</li>
      <li>List Item</li>
      <li>List Item</li>
    </ul>
  </div>
</div>
<!-- second accordion section -->
<!-- second parent accordion -->
<button class="accordion">Name of Parent Accordion</button>
<div class="panel">
  <!-- first nested accordion -->
  <button class="accordion">Name of Nested Accordion</button>
  <div class="panel">
    <ul>
      <li>List Item</li>
      <li>List Item</li>
      <li>List Item</li>
    </ul>
  </div>
</div>

<script>
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
    acc[i].addEventListener("click", function() {
        this.classList.toggle("active");
        var panel = this.nextElementSibling;
        if (panel.style.display === "block") {
            panel.style.display = "none";
        } else {
            panel.style.display = "block";
        }
    });
}
</script>

</body>
</html>