简体   繁体   中英

Bootstrap Accordion - Toggle all nested elements

I have a lot of nested bootstrap collapsible elements on a page. As example http://jsfiddle.net/JPgLT/7/

How can i set a link to toggle all elements only within the parent element?

<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>

You could add a special class closeall to the buttons, and then toggle the collapse state using 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
});

Demo: http://bootply.com/117082

Related questions
bootstrap 3 collapse('hide') opens all collapsibles?

I think I understood your problem a little differently, and I know you've already excepted an answer, but I thought I'd answer anyway as I'm pretty new to jQuery and would appreciate feedback

$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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM