18910140161

JavaScript-如何在展开另一个可折叠元素时关闭折叠部分?-堆栈溢出

顺晟科技

2022-10-19 13:52:06

176

我目前在bootstrap 5中创建可折叠元素时遇到了一些麻烦。我想要的是当单击不同的可折叠元素时关闭其他部分(类似于手风琴)。我需要它,以便一次只能打开一个部分。

引导折叠元素整洁;然而,我在他们的文档网站上找不到一个当一个内容被展开时其他内容折叠的例子。我做了一些研究,很多人都说要使用data-parent元素,但不幸的是,我没有使用它。

假设我希望能够通过一个bootstrapp示例实现这一点:

<p>
  <a class="btn btn-primary" data-bs-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
    Link with href
  </a>
  <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
    Button with data-bs-target
  </button>
</p>
<div class="collapse" id="collapseExample">
  <div class="card card-body">
    Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
  </div>
</div>

当页面上的其他折叠元素被击中时,如何确保打开的节关闭?此代码示例是本页上的第一个示例:https://getbootstrap.com/docs/5.0/components/collaps/

非常感谢您的帮助。


顺晟科技:

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航