我有一个引导3崩溃的问题,以编程方式打开一个面板,可以在重新打开第一个面板时保持另一个面板打开.
我的HTML:
<button type="button" class="btn showpanel">Show panel 3</button> <button type="button" class="btn hidepanel">Hide panel 3</button> <button type="button" class="btn openpanel">Open panel 3</button> <button type="button" class="btn closepanel">Close panel 3</button> <hr/> <div class="panel-group" id="accordion"> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#panel1">Panel 1</a> </h4> </div> <div id="panel1" class="panel-collapse collapse"> <div class="panel-body"> Contents panel 1 </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#panel2">Panel 2</a> </h4> </div> <div id="panel2" class="panel-collapse collapse"> <div class="panel-body"> Contents panel 2 </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#panel3">Panel 3</a> </h4> </div> <div id="panel3" class="panel-collapse collapse"> <div class="panel-body"> Contents panel 3 </div> </div> </div> </div>
和一些JavaScript:
$(".hidepanel").on("click",function() { $("#panel3").parent().hide(); }); $(".showpanel").on("click",function() { $("#panel3").parent().show(); }); $(".openpanel").on("click",function() { $("#panel3").collapse('show'); }); $(".closepanel").on("click",function() { $("#panel3").collapse('hide'); });
My jsfiddle that demonstrates this issue
复制:
>点击“打开面板3”按钮.
>点击“面板2”打开它.到目前为止没有问题.
>单击“面板3”再次打开.面板2保持打开!
所以以编程方式开设面板似乎搞砸了关于面板状态的内部注册?我没有看到第三个小组的“状态变化”有什么明显的错误(它的阶级从“崩溃”变为“in”,并且按照你的预期).
解决方法
您可以为崩溃显示事件创建一个处理程序,该事件恰好在任何面板显示之前发生.
$('#accordion').on('show.bs.collapse',function () { $('#accordion .in').collapse('hide'); });
您可以在这里阅读有关崩溃事件的更多信息:http://getbootstrap.com/javascript/#collapse