打开新div时如何关闭可折叠div

我正在尝试设置可折叠的 div。它们工作正常,但是当我打开另一个可折叠 div 时,打开的 div 不会关闭,除非我手动将它们一一关闭。


如何在打开另一个 div 时自动创建一个打开的可折叠 div?


超文本标记语言


<html>

  <button type="button" class="collapsible">BUTTON</button>

  <div class="content">

    <p style="padding: 20px 0;">CONTENT</p>

  </div>

  <button type="button" class="collapsible">BUTTON</button>

  <div class="content">

    <p style="padding: 20px 0;">CONTENT</p>

  </div>

  <button type="button" class="collapsible">BUTTON</button>

  <div class="content">

    <p style="padding: 20px 0;">CONTENT</p>

  </div>

</html>

JS


var coll = document.getElementsByClassName("collapsible");

var i;

for (i = 0; i < coll.length; i++) {

  coll[i].addEventListener("click", function() {

    this.classList.toggle("active");

    var content = this.nextElementSibling;

    if (content.style.maxHeight){

      content.style.maxHeight = null;

    } else {

      content.style.maxHeight = content.scrollHeight + "px";

    }

  });

}


互换的青春
浏览 46回答 2
2回答

哆啦的时光机

在打开折叠的 div 之前,您的代码必须关闭所有打开的 div。// toggle collapse of specified contentfunction toggleContent(content) {&nbsp; if (content.style.maxHeight) {&nbsp; &nbsp; content.style.maxHeight = null;&nbsp; } else {&nbsp; &nbsp; content.style.maxHeight = content.scrollHeight + 'px';&nbsp; }}// collapse all open contentfunction collapseAllOpenContent() {&nbsp; const colls = document.getElementsByClassName('collapsible');&nbsp; for (const coll of colls) {&nbsp; &nbsp; if (coll.classList.contains('active')) {&nbsp; &nbsp; &nbsp; coll.classList.remove('active');&nbsp; &nbsp; &nbsp; toggleContent(coll.nextElementSibling);&nbsp; &nbsp; }&nbsp; }}工作示例:// toggle collapse of specified contentfunction toggleContent(content) {&nbsp; if (content.style.maxHeight) {&nbsp; &nbsp; content.style.maxHeight = null;&nbsp; } else {&nbsp; &nbsp; content.style.maxHeight = content.scrollHeight + 'px';&nbsp; }}// collapse all open contentfunction collapseAllOpenContent() {&nbsp; const colls = document.getElementsByClassName('collapsible');&nbsp; for (const coll of colls) {&nbsp; &nbsp; if (coll.classList.contains('active')) {&nbsp; &nbsp; &nbsp; coll.classList.remove('active');&nbsp; &nbsp; &nbsp; toggleContent(coll.nextElementSibling);&nbsp; &nbsp; }&nbsp; }}const colls = document.getElementsByClassName('collapsible');for (const coll of colls) {&nbsp; coll.addEventListener('click', function() {&nbsp; &nbsp; if (!this.classList.contains('active')) {&nbsp; &nbsp; &nbsp; collapseAllOpenContent();&nbsp; &nbsp; }&nbsp; &nbsp; this.classList.toggle('active');&nbsp; &nbsp; toggleContent(this.nextElementSibling);&nbsp; });}.collapsible {&nbsp; background-color: #fff;&nbsp; color: #555;&nbsp; cursor: pointer;&nbsp; padding: 18px;&nbsp; width: 100%;&nbsp; border: none;&nbsp; text-align: left;&nbsp; outline: none;&nbsp; font-size: 15px;}.active,.collapsible:hover {&nbsp; background-color: #fff;}.collapsible:after {&nbsp; content: '\002B';&nbsp; color: #555;&nbsp; font-weight: bold;&nbsp; float: right;&nbsp; margin-left: 5px;}.active:after {&nbsp; content: "\2212";}.content {&nbsp; padding: 0 18px;&nbsp; max-height: 0;&nbsp; overflow: hidden;&nbsp; transition: max-height 0.2s ease-out;&nbsp; background-color: #fff;}<button type="button" class="collapsible">BUTTON</button><div class="content">&nbsp; <p style="padding: 20px 0;">CONTENT</p></div><button type="button" class="collapsible">BUTTON</button><div class="content">&nbsp; <p style="padding: 20px 0;">CONTENT</p></div><button type="button" class="collapsible">BUTTON</button><div class="content">&nbsp; <p style="padding: 20px 0;">CONTENT</p></div>

慕斯709654

人们对这种方法有很多担忧......关注点分离,例如单独的样式与模板(CSS 与 HTML)。将所有样式移至单独的 css 文件中。也在 JS 中设置样式,而不是使用 CSS 类,您可以在其中添加所需的样式,例如 maxHeight、padding 等。不要在代码中使用语义 HTML。例如,使用详细信息作为包装器,使用摘要作为包装器的描述,并使用按钮来切换 p 内容。根据document.getElementsByClassName("collapsible")集合的长度添加多个点击事件,这在性能方面不太好。您可以在 document.body 上添加事件,然后验证单击的元素是否属于给定类型并具有给定的类/id。但是,此时此刻,您甚至不再需要 JS 来完成此类功能。您需要做的就是使用details和summary标签,它将以这种手风琴方式运行。<details>&nbsp; &nbsp; <summary>Accordion item title 1</summary>&nbsp; &nbsp; <div class="accordion-content">&nbsp; &nbsp; &nbsp; content for the second item&nbsp; &nbsp; </div></details><details>&nbsp; &nbsp; <summary>Accordion item title 2</summary>&nbsp; &nbsp; <div class="accordion-content">&nbsp; &nbsp; &nbsp; content for the first item&nbsp; &nbsp; </div></details>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5