【问题标题】:collpase and expand all bootstrap 3 accordion折叠和展开所有 bootstrap 3 手风琴
【发布时间】:2015-11-29 00:41:11
【问题描述】:

我的代码可以工作到: 当我单击折叠或展开全部时,它会起作用并将按钮的文本从全部折叠更改为全部展开。唯一的问题是:当我通过单击标题打开或关闭所有面板时,它也应该更改按钮的文本。

我希望我已经解决了足够多的问题。

我没有在这个例子中包含 Jquery 和 bootstrap 外部文件。但您可以在以下链接中找到示例。

jsfiddle example here.

jQuery

     $('.expand-all').click(function(){
     var $this = $(this);
     $this.toggleClass('collapse-all');
     if($this.hasClass('collapse-all')){
     $this.text(' Expand All');
     $('.panel-collapse.in')
     .collapse('hide');
     } 
     else {
     $this.text('Collapse All');
     $('.panel-collapse:not(".in")')
     .collapse('show');
     }
     });

HTML

 <div class="container">
                    <!-- <a href="#" class="btn btn-default openall">open all</a> <a href="#" class="btn btn-default closeall">close all</a>--><a href="#" class="btn btn-default expand-all">Collapse All</a>
                    <hr>
                    <div class="panel-group" id="accordion">
                        <div class="panel panel-default">
                            <div class="panel-heading" data-toggle="collapse" data-parent="#accordion1" href="#collapseOne">
                                <h4 class="panel-title">
                                    <a class="" >
                                        Collapsible Group Item #1
                                    </a>
                                </h4>
                            </div>
                            <div id="collapseOne" class="panel-collapse collapse in">
                                <div class="panel-body">
                                    ONe
                                </div>
                            </div>
                        </div>
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <h4 class="panel-title">
                                    <a class="" data-toggle="collapse" data-parent="#accordion1" href="#collapseTwo">
                                        Collapsible Group Item #2
                                    </a>
                                </h4>
                            </div>
                            <div id="collapseTwo" class="panel-collapse collapse in">
                                <div class="panel-body">
                                    Two Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                                </div>
                            </div>
                        </div>
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <h4 class="panel-title">
                                    <a class="" data-toggle="collapse" data-parent="#accordion1" href="#collapseThree">
                                        Collapsible Group Item #3
                                    </a>
                                </h4>
                            </div>
                            <div id="collapseThree" class="panel-collapse collapse in">
                                <div class="panel-body">
                                    Three Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

【问题讨论】:

    标签: javascript jquery html css twitter-bootstrap


    【解决方案1】:

    this怎么样?

    代码:

    $('.expand-all').click(function(){
        var $this = $(this);
        if($this.hasClass('collapse-all')){
            $this.text(' Expand All');
            $('.panel-collapse.in').collapse('hide');
            $this.removeClass('collapse-all');
        } else {
            $this.text('Collapse All');
            $('.panel-collapse:not(".in")').collapse('show');
            $this.addClass('collapse-all');
        }
    });
    
    $('#accordion').on('show.bs.collapse hide.bs.collapse', function (obj) {
        var $this = $(this);
        var $expand = $('.expand-all');
        if(obj.type=="show"){
            $expand.text('Collapse All').addClass('collapse-all');
        }
        else{
            if($('.collapse.in').not(obj.target).length == 0){
                $expand.text('Expand All').removeClass('collapse-all');
            }
        }
    });
    

    还将collapse-all 类添加到按钮

    <a href="#" class="btn btn-default expand-all collapse-all">Collapse All</a>
    

    【讨论】:

      【解决方案2】:

      这将解决您的问题

      $(".panel-heading a").click(function(){
          setTimeout(function(){
              if($(".collapsed").length == $(".panel-heading").length){
                  $(".btn-default").text(' Expand All');
                if(!$('.expand-all').hasClass('collapse-all')){
                     $('.expand-all').addClass('collapse-all')
                 }
              }else{
                  $(".btn-default").text('Collapse All');
              }
          },100)    
      })
      

      这里是小提琴http://jsfiddle.net/26hu7o5y/4/

      【讨论】:

      • 谢谢你或你的快速回答。但问题是现在。当它展开时,我需要单击两次以使其展开。甚至按钮显示全部展开。同样,我手动打开所有面板(两次不单击按钮)我需要单击按钮两次。
      • 这里修复jsfiddle.net/26hu7o5y/4 看看,让我知道这是正确的
      猜你喜欢
      • 2021-11-10
      • 2016-09-25
      • 2020-01-25
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多