Bootstrap Accordion - 切换所有嵌套元素

时间:2014-02-26 23:12:15

标签: javascript jquery twitter-bootstrap collapse

我在页面上有很多嵌套的bootstrap可折叠元素。 例如http://jsfiddle.net/JPgLT/7/

如何设置链接以仅在父元素中切换所有元素?

<div class="panel panel-default">
    <div class="panel-heading">
            <h2 class="panel-title">
                <a href="#collapse-764" data-parent="#collapsible-" class="accordion-toggle" data-toggle="collapse">Headline 1</a>
            </h2>
    </div>
    <div id="collapse-764" class="panel-collapse collapse">
        <div class="panel-body">
            <div class="panel panel-default">
                <div class="panel-heading">
                        <h2 class="csc-firstHeader panel-title">
                            <a href="#collapse-765" data-parent="#collapsible-764" class="accordion-toggle" data-toggle="collapse">Headline 1.1</a>
                        </h2>
                </div>
                <div id="collapse-765" class="panel-collapse collapse">
                    <div class="panel-body">
                        <p>Text 1.1</p>
                    </div>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h2 class="panel-title">
                        <a href="#collapse-766" data-parent="#collapsible-764" class="accordion-toggle" data-toggle="collapse">Headline 1.2</a>
                    </h2>
                </div>
                <div id="collapse-766" class="panel-collapse collapse">
                    <div class="panel-body">
                        <p>Text 1.2</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

您可以在按钮上添加一个特殊的类closeall,然后使用jQuery切换折叠状态。

$('.closeall').click(function(){
    $(this).parents('.panel-heading') // find the parent heading element
    .next('.panel-collapse')          // find the next collapse element
    .collapse('show')                 // make sure it's open
    .find('.panel-collapse')          // find inner collapse elements
    .collapse('toggle');              // toggle their state
});

演示:http://bootply.com/117082

相关问题
bootstrap 3 collapse('hide') opens all collapsibles?

答案 1 :(得分:0)

我认为我对你的问题的理解有所不同,我知道你已经排除了答案,但我认为无论如何我都会回答,因为我对jQuery很新,并希望得到反馈

$el = $(".accordian-toggle").first();  //Use whatever selector you have to to select the parent element
$el.click(); //expand/toggle that element

//then use that elements href attribute as the selector to find all toggleable 'child' elements 
$($el.attr('href')).find(".accordion-toggle").each(function(){
    $(this).click();
});