我在手风琴中使用Bootstrap 4.0的折叠组件,类似于他们docs上的手风琴。
<div id="accordion">
<div class="card">
<div class="card-header" id="headingOne">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Collapsible Group Item #1
</button>
</h5>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
<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. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingTwo">
<h5 class="mb-0">
<button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Collapsible Group Item #2
</button>
</h5>
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordion">
<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. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingThree">
<h5 class="mb-0">
<button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Collapsible Group Item #3
</button>
</h5>
</div>
<div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordion">
<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. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div>
</div>
</div>
</div>
因此默认情况下会扩展项目#1。然后当你点击第3项时,#1关闭,#3扩展......完美!
但是,默认操作是,如果展开第3项并单击该标题,则面板将关闭,您只剩下标题列表。我想更进一步,如果我们关闭第3项,那么第1项将会扩展,因此它将成为默认的&#39;如果没有进行其他选择,将会打开的小组。
我已经看到了solution的Bootstrap3,其中一个面板始终处于打开状态,但我希望能够将一个特定的面板(项目#1)作为打开的面板备份。这是易于参考的脚本:
$('.panel-heading a').on('click',function(e){
if($(this).parents('.panel').children('.panel-collapse').hasClass('in')){
e.stopPropagation();
}
// You can also add preventDefault to remove the anchor behavior that makes
// the page jump
// e.preventDefault();
});
答案 0 :(得分:1)
您可以使用折叠隐藏事件。在这种情况下,请使用.eq(0)
,因为0是第一个可折叠div的索引。
$('.collapse').on('hidden.bs.collapse', function () {
$('.collapse').eq(0).collapse('show');
})
为了更进一步,您可以将新的default
数据变量添加到父级#accordion ...
<div id="accordion" class="accordion" data-default="1">..</div>
然后,更改jQuery以使用该变量..
/* default accordion variable method */
$('.collapse').on('hidden.bs.collapse', function () {
var defaultDiv = $($(this).data("parent")).data("default");
$('.collapse').eq(defaultDiv-1).collapse('show');
})
演示: https://www.codeply.com/go/NilPIQD9oi
另一种选择是防止任何开放式手风琴自行关闭,如explained in this answer。
答案 1 :(得分:0)
您可以尝试
jQuery(function($){
$('[data-toggle=collapse]').on('click', function (e) {
e.preventDefault();
if(! $(this).hasClass('collapsed')){
e.stopPropagation();
return false;
}
});
});