twitter-bootstrap - bootstrap 3 collapse ('hide' )打开所有可折叠的?

标签 twitter-bootstrap twitter-bootstrap-3

我正在尝试实现一些相当简单的事情,但无法理解它 - 谷歌没有任何帮助,而且 Bootstrap 文档似乎有点令人困惑。

我需要的:

  • 简单的可折叠 Accordion ,所有可折叠在页面加载时处于关闭状态
  • 只需单击一个按钮即可关闭所有可折叠项。

  • 问题:
  • 我的解决方案仅在用户打开/关闭一个或多个可折叠文件时才有效
  • 当我调用 collapse('hide') , 所有可折叠元素都已打开 ,除非它们之前已经手动打开过。

  • Jsfiddle .

    我的标记:
    <a class="btn btn-default" id="collapseall"><span class="icon"></span>Collapse 'm all!</a>
    
    <div class="panel-group" id="accordion">
      <div class="panel panel-default">
        <div class="panel-heading">
          <h4 class="panel-title">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
              Collapsible Group Item #1
            </a>
          </h4>
        </div>
        <div id="collapseOne" class="panel-collapse collapse">
          <div class="panel-body">
            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 data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
              Collapsible Group Item #2
            </a>
          </h4>
        </div>
        <div id="collapseTwo" class="panel-collapse collapse">
          <div class="panel-body">
            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 data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
              Collapsible Group Item #3
            </a>
          </h4>
        </div>
        <div id="collapseThree" class="panel-collapse collapse">
          <div class="panel-body">
            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>
    

    Javascript:
    $('#collapseall').click(function () {
        $('.panel-collapse').collapse('hide');
        return false;
    });
    

    我在这里想念什么?

    最佳答案

    我想你只需要 hide像这样的开放的..

    $('#collapseall').click(function(){
      $('.panel-collapse.in')
        .collapse('hide');
    });
    

    这是一个有效的打开/关闭所有示例:http://bootply.com/123636

    关于twitter-bootstrap - bootstrap 3 collapse ('hide' )打开所有可折叠的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22966170/

    相关文章:

    jquery - ajax 调用后 Bootstrap 下拉列表不起作用(即使在初始化后)

    html - 如何在div中居中<i>标签

    twitter-bootstrap - 使用 Bootstrap 3 和 Respond.js 在 IE8 中包装列

    jquery - Paypal 按钮定制 Bootstrap 喜欢

    javascript - Twitter bootstrap typeahead 自定义按键 ENTER 功能

    html - 如何消除导航栏和容器流体之间的差距?

    css - 如何在 Bootstrap 中使用垂直对齐

    css - 使用 bootstrap 的响应式 css 规则

    javascript - 无法在 Bootstrap 中实现 W3Schools 幻灯片放映?

    css - 如何修改此 Bootstrap 列堆叠行为