Bootstrap 3可折叠面板,没有id或数据

时间:2014-08-27 10:45:32

标签: javascript twitter-bootstrap-3 panel collapse

我需要在不使用ID或数据属性的情况下处理面板折叠行为。理想情况下,通过设置"可折叠"父div上可以折叠其内容的类,单击标题应该触发崩溃

这里是我想写的目标HTML以自动处理崩溃行为

<div class="panel-group collapse-all-but-one">
    <div class="panel panel-primary panel-collapsible>
        <div class="panel-heading">Click me to collapse</div>
        <div class="panel-body">I will collaaaaaaapse</div>
    </div>
    <div class="panel panel-warning panel-collapsible">
        <div class="panel-heading">Hey another one</div>
        <div class="panel-body">I will close when the other one opens</div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

这是我的神奇代码(准备好的东西确保与rails / angularJS兼容)

var ready
ready = function(){
    $(".panel-collapsable")
    .children(".panel-heading")
        .click(function(){
            var group = $(this).parent().parent()
            if (group.hasClass("panel-group one-at-a-time")){
                group.children(".panel").children(".panel-body")
                    .collapse('hide')
            }
            $(this)
                .next('.panel-body')
                .collapse('toggle')
        })
}
$(document).ready(ready);
$(document).on('page:load', ready);

请记住添加一些引导类,如.collapse .in或.collapsed来初始化正确的行为,或者第一次点击不会做任何事情。

<div class="panel-body collapse in">I am expanded on page load</div>
...
<div class="panel-body collapse">I am collapsed on page load</div>