保持多个折叠打开(使用按钮切换)– Bootstrap 3

Posted

技术标签:

【中文标题】保持多个折叠打开(使用按钮切换)– Bootstrap 3【英文标题】:Keep multiple collapse open (with button to toggle) – Bootstrap 3 【发布时间】:2016-01-08 20:41:22 【问题描述】:

我正在尝试使多个手风琴可以同时打开,但随后也可以使用导航栏中的按钮打开/关闭所有手风琴。

一般建议似乎是删除 data-parent,我已经这样做了,但仍然存在冲突。

任何帮助表示赞赏。

<!DOCTYPE html>
<html lang="en">
  <head>
   <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Well Placed</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/custom.css" rel="stylesheet">
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:700|Libre+Baskerville' rel='stylesheet' type='text/css'>
   
  </head>

  <body>

    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand tk-brandon-grotesque" href="#">Well Placed</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li><a href="#">Home</a></li>
            <li><a href="#contact">Contact</a></li>
            <li><p id="collapse-init" class="navbar-text">Open All</p></li>
            
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container">

 

<div class="accordion" id="accordion">
<!--Article 1-->
                <div class="accordion-group">
                  <div class="accordion-heading">
                    <a class="accordion-toggle" data-toggle="collapse" href="#collapseOne">
                      Collapsible Group Item #1
                    </a>
                  </div>
                  <div id="collapseOne" class="accordion-body collapse">
                    <div class="accordion-inner">
                      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>
<!--Article 2-->
                <div class="accordion-group">
                  <div class="accordion-heading">
                    <a class="accordion-toggle" data-toggle="collapse" href="#collapseTwo">
                      Collapsible Group Item #2
                    </a>
                  </div>
                  <div id="collapseTwo" class="accordion-body collapse">
                    <div class="accordion-inner">
                      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>
<!--Article 3-->
                <div class="accordion-group">
                  <div class="accordion-heading">
                    <a class="accordion-toggle" data-toggle="collapse" href="#collapseThree">
                      Collapsible Group Item #3
                    </a>
                  </div>
                  <div id="collapseThree" class="accordion-body collapse">
                    <div class="accordion-inner">
                      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>
<!--Article 4-->
                <div class="accordion-group">
                  <div class="accordion-heading">
                    <a class="accordion-toggle" data-toggle="collapse" href="#collapseFour">
                      Collapsible Group Item #4
                    </a>
                  </div>
                  <div id="collapseFour" class="accordion-body collapse">
                    <div class="accordion-inner">
                      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><!-- /.container -->


   <!-- jQuery (necessary for Bootstrap's javascript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
   <!-- Script for the hide all / show all -->
    <script type="text/javascript">
     $(function () 

    var active = true;

    $('#collapse-init').click(function () 
        if (active) 
            active = false;
            $('.accordion-body').collapse('show');
            $('.accordion-heading').attr('data-toggle', '');
            $(this).text('Close All');
         else 
            active = true;
            $('.accordion-body').collapse('hide');
            $('.accordion-heading').attr('data-toggle', 'collapse');
            $(this).text('Open All');
        
    );
    
    $('#accordion').on('show.bs.collapse', function () 
        if (active) $('#accordion .in').collapse('hide');
    );

);
    </script>
  </body>
</html>

【问题讨论】:

【参考方案1】:

我认为您想离开data-parent,并使用in 类来确定哪些是活动的。然后在“collapse-init”上使用另一个类(即活动)来切换打开/关闭所有..

http://bootply.com/WiT2C1Cm5d

【讨论】:

以上是关于保持多个折叠打开(使用按钮切换)– Bootstrap 3的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap 4.6 折叠不切换(打开或关闭)

修改 Twitter Bootstrap 折叠插件以保持手风琴打开

(十六) 按钮和折叠插件

在 Reactjs 中切换多个元素的状态

以编程方式打开面板后,Bootstrap 3 折叠可以打开多个面板

Material-UI 中的多个展开/折叠 TableRow 组件