我正在尝试添加一个手风琴,但是它没有打开

时间:2019-12-08 22:43:36

标签: javascript jquery html css accordion

单击按钮后手风琴没有打开。

请帮助。

非常感谢!

请找到下面编写的代码: 请找到以下代码: 请找到以下代码: 请找到以下代码: 请找到以下代码: 请找到以下代码: 请找到以下代码: 请找到以下代码: 请找到以下代码: 请找到以下代码: 请找到以下代码: 请找到下面写的代码:

请找到下面编写的代码:

< script >
  document.querySelectorAll("accordion__button").forEach(button => {
    button.addEventListener("click", () => {
      const accordionContent = button.nextElementSibling;
      button.classList.toggle("accordion__button--active");
      if (button.classList.contains("accordion__button--active")) {
        accordionContent.style.maxHeight = accordionContent.scrollHeight + 'px';
      } else {
        accordionContent.style.maxHeight = 0;
      }
    });
  });

</script>
.accordion__button {
  display: block;
  width: 100%;
  padding: 15px;
  border: none;
  outline: none;
  cursor: pointer;
  background: #333333;
  color: #ffffff;
  text-align: left;
  transition: background 0.2s;
}

.accordion__button::after {
  content: '\25be';
  float: right;
  transform: scale(1.5);
}

.accordion__button--active {
  background: #555555;
}

.accordion__button--active::after {
  content: '\25be';
}

.accordion__content {
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.2s;
  padding: 0 15px;
  font-family: sans-serif;
  background: #eeeeee;
}
<div class="accordion">
  <button type="button" class="accordion__button">FUP Plans</button>
  <div class="accordion__content">
    <p>"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia </p>
  </div>
</div>

<script>  document.querySelectorAll("accordion__button").forEach(button => {
            button.addEventListener("click", () => {
                const accordionContent = button.nextElementSibling;
                button.classList.toggle("accordion__button--active");
                if (button.classList.contains("accordion__button--active")) {
                    accordionContent.style.maxHeight = accordionContent.scrollHeight + 'px';
                }
                else {
                    accordionContent.style.maxHeight = 0;
                }
            });
        });

</script>
.accordion__button {
    display: block;
    width: 100%;
    padding: 15px;
    border: none;
    outline: none;
    cursor: pointer;
    background: #333333;
    color: #ffffff;
    text-align: left;
    transition: background 0.2s;
}

.accordion__button::after {
    content: '\25be';
    float: right;
    transform: scale(1.5);
}

.accordion__button--active {
    background: #555555;
}

.accordion__button--active::after {
    content: '\25be';
}

.accordion__content {
    overflow: hidden;
    max-height: 0;
    transition: max-height 0.2s;
    padding: 0 15px;
    font-family: sans-serif;
    background: #eeeeee;
}
<div class="accordion">
        <button type="button" class="accordion__button">FUP Plans</button>
        <div class="accordion__content" >
            <p>"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia
 </p>
        </div>
    </div>

enter code here

1 个答案:

答案 0 :(得分:2)

问题在于修改后的行。

  document.querySelectorAll(".accordion__button").forEach(button => { ... });

只需添加“。”定义元素类名的字符。