【问题标题】:JavaScript Accordion script with multiple classes具有多个类的JavaScript手风琴脚本
【发布时间】:2018-11-24 08:21:30
【问题描述】:

我不是 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>

【问题讨论】:

  • 类是你的朋友,不要害怕使用它们。它们可用于识别级别,并使您的生活更轻松。即class='accordion parent'class='accordion nested'
  • 您好,感谢您的评论。我试图在这个实例中使用多个类来识别不同级别的手风琴,但我还没有弄清楚如何正确地将它们合并到我的 JS 中。
  • 这可能表明我缺乏经验,但我没有意识到您可以将类命名为:class='accordion parent' 和 class='accordion nested',然后在 CSS 手风琴.parent 和手风琴中调用它们。嵌套。每天都能学到新东西,谢谢。

标签: javascript jquery html css accordion


【解决方案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>

【讨论】:

  • 感谢您的建议,我已尝试使用此脚本,但它似乎不允许我扩展我的父手风琴。
  • 在使用它之后,这似乎解决了我的问题,非常感谢。
猜你喜欢
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多