javascript - 如何一次只显示一个可折叠元素?
问题描述
一切尽在标题中。基本上,我正在使用此Bootstrap 页面中的第一个示例。此页面上没有显示如何在单击并展开不同元素时折叠其他元素的示例。我基本上希望它能够像手风琴一样工作,当您单击另一个元素时,您打开的元素会被隐藏(关闭),而您单击的元素会展开。请帮我解决这个问题,我真的很想弄清楚,但我很挣扎。这是来自 Bootstrap 5 的一些演示代码。我将如何得到这个,所以这是两个具有独特内容的独特元素,当您单击一个时,它会展开,而另一个打开的则关闭。非常感谢您的帮助 - 下面的演示代码 - 上面附有此示例的链接。这两个按钮都有独特的内容,但它们都可以同时打开——我不想要这个,我只想要一次打开一个,另一个在点击时关闭。谢谢 - 任何解决方案表示赞赏
<p>
<a class="btn btn-primary" data-bs-toggle="collapse" href="#multiCollapseExample1" role="button" aria-expanded="false" aria-controls="multiCollapseExample1">Toggle first element</a>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#multiCollapseExample2" aria-expanded="false" aria-controls="multiCollapseExample2">Toggle second element</button>
</p>
<div class="row">
<div class="col">
<div class="collapse multi-collapse" id="multiCollapseExample1">
<div class="card card-body">
Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
<div class="col">
<div class="collapse multi-collapse" id="multiCollapseExample2">
<div class="card card-body">
Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
</div>
解决方案
用 id 将你的折叠包裹在一个 div 中。并使用带有父 ID 的 data-target 属性。
<div id="abc">
<p>
<a
class="btn btn-primary"
data-bs-toggle="collapse"
href="#multiCollapseExample1"
role="button"
aria-expanded="false"
aria-controls="multiCollapseExample1"
>Toggle first element</a
>
<button
class="btn btn-primary"
type="button"
data-bs-toggle="collapse"
data-bs-target="#multiCollapseExample2"
aria-expanded="false"
aria-controls="multiCollapseExample2"
>
Toggle second element
</button>
</p>
<div class="row">
<div class="col">
<div
class="collapse multi-collapse"
id="multiCollapseExample1"
data-bs-parent="#abc"
>
<div class="card card-body">
Some placeholder content for the first collapse component of this
multi-collapse example. This panel is hidden by default but
revealed when the user activates the relevant trigger.
</div>
</div>
</div>
<div class="col">
<div
class="collapse multi-collapse"
id="multiCollapseExample2"
data-bs-parent="#abc"
>
<div class="card card-body">
Some placeholder content for the second collapse component of this
multi-collapse example. This panel is hidden by default but
revealed when the user activates the relevant trigger.
</div>
</div>
</div>
</div>
</div>
推荐阅读
- android - 为什么水平布局在更宽的分辨率手机上溢出但适合更薄的手机
- arrays - 在由动态分配数组制成的板上用 SRAND 更改 6 个不同的随机数
- omnet++ - 不能在停车场停车
- node.js - Dialogflow Fulfillment Webhook - 请求有效负载始终为空
- kubernetes - 确定哪个 GKE 节点正在处理客户端请求
- javascript - 如果只选择一个选项,如何在 Node 后端处理 HTML `select` 多个值
- opengl - 默认摄像头在哪里?
- javascript - 不能在模块外使用 import 语句 - React Native
- flutter - 如何在 Flutter(`beacon_broadcast` 库)中发现错误?常规方法不适用于此库
- java - 在 Java 中将十六进制字节数组的字符串表示形式转换为具有非 ascii 字符的字符串