Bootstrap: Collapse other sections when one is expanded

后端 未结 11 788
一向
一向 2020-11-27 06:28

I am making a Rails app, and am trying to achieve a particular functionality relating to Twitter\'s Bootstrap collapse. Bear with me as I explain it.

I currently hav

相关标签:
11条回答
  • 2020-11-27 06:51

    The Method Works Properly For me:

    var lanopt = $(".language-option");
    
    lanopt.on("show.bs.collapse",".collapse", function(){
       lanopt.find(".collapse.in").collapse("hide");
    });
    
    0 讨论(0)
  • 2020-11-27 06:52

    If you are using Bootstrap 4, and you don't want to change your markup:

    var $myGroup = $('#myGroup');
    $myGroup.on('show.bs.collapse','.collapse', function() {
    $myGroup.find('.collapse.show').collapse('hide');
    });
    
    0 讨论(0)
  • 2020-11-27 06:53

    In bootstrap 4 to close all collapseds works like this:

    ACTION:

    <button id="CloseAll" class="btn btn-primary" type="button" data-toggle="collapse">Close All</button>
    

    JQUERY:

    $(document).ready(function() {
        $("#CloseAll").on('click', function() {
            $(".collapse").removeClass("show");
        });
    });
    
    0 讨论(0)
  • 2020-11-27 06:57

    This was helpful for me:

        jQuery('button').click( function(e) {
        jQuery('.in').collapse('hide');
    });
    

    It's collapsed already open section. Thnks to GrafiCode Studio

    0 讨论(0)
  • 2020-11-27 06:59

    Using data-parent, first solution is to stick to the example selector architecture

    <div id="myGroup">
        <button class="btn dropdown" data-toggle="collapse" data-target="#keys" data-parent="#myGroup"><i class="icon-chevron-right"></i> Keys  <span class="badge badge-info pull-right">X</span></button>
        <button class="btn dropdown" data-toggle="collapse" data-target="#attrs" data-parent="#myGroup"><i class="icon-chevron-right"></i> Attributes</button>
        <button class="btn dropdown" data-toggle="collapse" data-target="#edit" data-parent="#myGroup"><i class="icon-chevron-right"></i> Edit Details</button>
    
        <div class="accordion-group">
            <div class="collapse indent" id="keys">
                keys
            </div>
    
            <div class="collapse indent" id="attrs">
                attrs
            </div>
    
            <div class="collapse" id="edit">
                edit
            </div>
        </div>
    </div>
    

    Demo (jsfiddle)

    Second solution is to bind on the events and hide the other collapsible elements yourself.

    var $myGroup = $('#myGroup');
    $myGroup.on('show.bs.collapse','.collapse', function() {
        $myGroup.find('.collapse.in').collapse('hide');
    });
    

    Demo (jsfiddle)

    PS: the strange effect in the demos is caused by the min-height set for the example, just ignore that.


    Edit: changed the JS event from show to show.bs.collapse as specified in Bootstrap documentation.

    0 讨论(0)
  • 2020-11-27 06:59

    working like a charm here for bootstrap 4>4.1.1

    var myGroup = $('your-list');
    
    myGroup.on('show.bs.collapse','.collapse', function() {
       myGroup.find('.collapse.show').collapse('hide');
    });
    
    0 讨论(0)
提交回复
热议问题