bootstrap3折叠面板如何在一个同时使用两个
时间: 2024-02-11 07:03:51 浏览: 154
Bootstrap创建可折叠的组件
Bootstrap 3折叠面板默认只能同时展开一个面板,如果要同时展开两个面板,可以通过以下两种方法实现:
方法1:使用自定义的JS代码。可以在每个面板的标题上添加一个自定义的data属性,用于标识该面板的唯一性。然后,使用jQuery选择器来控制展开和关闭面板的行为。具体代码如下:
```html
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne" data-panel="1">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Collapsible Group Item #1
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch.
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingTwo" data-panel="2">
<h4 class="panel-title">
<a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Collapsible Group Item #2
</a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch.
</div>
</div>
</div>
</div>
<script>
$(function() {
$('[data-toggle="collapse"]').click(function(e) {
var $panel = $(this).parents('.panel').first();
var panelNumber = $panel.find('.panel-heading').attr('data-panel');
var $target = $(this).attr('href');
if ($($target).hasClass('in') && $panel.siblings('.panel').find('.panel-collapse.in').length > 0) {
e.preventDefault();
} else {
$($target).collapse('toggle');
}
});
});
</script>
```
方法2:使用Bootstrap 4的折叠面板。Bootstrap 4的折叠面板支持在一个页面中同时展开多个面板,只需要在面板组的外层div上添加`data-accordion="false"`属性即可。具体代码如下:
```html
<div class="accordion" id="accordionExample" data-accordion="false">
<div class="card">
<div class="card-header" id="headingOne">
<h2 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Collapsible Group Item #1
</button>
</h2>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch.
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingTwo">
<h2 class="mb-0">
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Collapsible Group Item #2
</button>
</h2>
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
<div class="card-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch.
</div>
</div>
</div>
</div>
```
阅读全文