Bootstrap折叠。如何一次只扩展一个div

时间:2016-06-10 16:49:09

标签: jquery twitter-bootstrap collapse

我怎样才能一次展示一个?

演示:http://jsfiddle.net/tvvq59wv/

$('.collapser').click(function() {
  $(this).next().collapse('toggle');
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<div id="myGroup">
  <div aria-controls="collapseExample" aria-expanded="false" data-toggle="collapse" class=" row collapsed collapser" style="background: #ddd;">
    <div class="col-md-4 col-xs-4">asfa asf asfasf afsf afsasf asf asf asf adf</div>
    <div class="col-md-4 col-xs-4">test</div>
    <div class="col-md-4 col-xs-4" style="text-align: right;">asf afsas afsasf asf</div>
  </div>

  <div id="collapseExample" class="collapse" style="height: 0px;">
    <div class="well">asf t1</div>
  </div>

  <div aria-controls="collapseExample" aria-expanded="false" data-toggle="collapse" class=" row collapsed collapser" style="background: #ddd;">
    <div class="col-md-4 col-xs-4">asfa afsasf</div>
    <div class="col-md-4 col-xs-4">test sd sdgs sd asf asfas afasf asfasfgd</div>
    <div class="col-md-4 col-xs-4" style="text-align: right;">asf afsas afsasf asf</div>
  </div>

  <div id="collapseExample" class="collapse" style="height: 0px;">
    <div class="well">asf t1</div>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

提前致谢!

1 个答案:

答案 0 :(得分:8)

Bootstrap手风琴。 jQuery vs HTML-attributes

有两种方法可以解决您的问题。您可以使用Javascript或分配HTML属性。但首先我们要简化代码。

起点

  1. col-md-4 col-xs-4等于col-xs-4
  2. Bootstrap包含alignment classes。您可以使用text-right类代替style="text-align: right;"
  3. 请注意,.row类具有属性margin-right: -15px; margin-left: -15px;。您.row.container
  4. 中的need to place .container-fluid
  5. style="height: 0px;"是不必要的。 collapse类将display属性设置为none
  6. id必须是唯一的。
  7. 让我们从这段代码开始:

    https://jsfiddle.net/glebkema/a5q9mgho/

    &#13;
    &#13;
    @import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
    
    .text {
      margin-bottom: 20px;
      padding: 15px;
    }
    
    .mauve { background: #c9f }
    .mint  { background: #9fc }
    .peach { background: #fc9 }
    
    .text.mauve { background: #edf }
    .text.mint  { background: #dfe }
    .text.peach { background: #fed }
    &#13;
    <div id="myGroup" class="container">
    
      <div class="row mint">
        <div class="col-xs-4">left</div>
        <div class="col-xs-4 text-center">center</div>
        <div class="col-xs-4 text-right">right</div>
      </div>
      <div class="row">
        <div class="text mint">text</div>
      </div>
    
      <div class="row mauve">
        <div class="col-xs-4">left</div>
        <div class="col-xs-4 text-center">center</div>
        <div class="col-xs-4 text-right">right</div>
      </div>
      <div class="row">
        <div class="text mauve">text</div>
      </div>
    
      <div class="row peach">
        <div class="col-xs-4">left</div>
        <div class="col-xs-4 text-center">center</div>
        <div class="col-xs-4 text-right">right</div>
      </div>
      <div class="row">
        <div class="text peach">text</div>
      </div>
    
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    &#13;
    &#13;
    &#13;

    通过jQuery

    1. .toggle类添加到行。这些块将切换相邻块的状态。
    2. 使用.collapse类使块可折叠。
    3. 该脚本执行两项操作:
      • 隐藏除下一个之外的所有扩展div。
      • 切换下一个div。
    4. http://jsfiddle.net/glebkema/73gtkvjt/

      &#13;
      &#13;
      $('.toggle').click(function() {
        if ( !$(this).next().hasClass('in') ) {
          $(this).parent().children('.collapse.in').collapse('hide');
        }
        $(this).next().collapse('toggle');
      });
      &#13;
      @import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
      
      .text {
        margin-bottom: 20px;
        padding: 15px;
      }
      
      .mauve { background: #c9f }
      .mint  { background: #9fc }
      .peach { background: #fc9 }
      
      .text.mauve { background: #edf }
      .text.mint  { background: #dfe }
      .text.peach { background: #fed }
      &#13;
      <div id="myGroup" class="container">
      
        <div class="row mint toggle">
          <div class="col-xs-4">left</div>
          <div class="col-xs-4 text-center">center</div>
          <div class="col-xs-4 text-right">right</div>
        </div>
        <div class="row collapse in">
          <div class="text mint">text</div>
        </div>
      
        <div class="row mauve toggle">
          <div class="col-xs-4">left</div>
          <div class="col-xs-4 text-center">center</div>
          <div class="col-xs-4 text-right">right</div>
        </div>
        <div class="row collapse">
          <div class="text mauve">text</div>
        </div>
      
        <div class="row peach toggle">
          <div class="col-xs-4">left</div>
          <div class="col-xs-4 text-center">center</div>
          <div class="col-xs-4 text-right">right</div>
        </div>
        <div class="row collapse">
          <div class="text peach">text</div>
        </div>
      
      </div>
      
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
      &#13;
      &#13;
      &#13;

      按HTML属性

      N.B。此方法有效in conjunction the panel component。可折叠块必须是块的子节点,其具有panel类。

      1. <div class="panel"></div>
      2. 中的所有块换行
      3. 使用.collapse类使块可折叠。为这些块添加唯一id s。
      4. a set of attributes添加到每个切换块:
      5. role="button" data-toggle="collapse" data-parent="#myGroup" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"
        

        https://jsfiddle.net/glebkema/L02ao1n9/

        &#13;
        &#13;
        @import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
        
        .panel {
          border: 0;
          margin-bottom: 0;
        }
        
        .text {
          margin-bottom: 20px;
          padding: 15px;
        }
        
        .mauve { background: #c9f }
        .mint  { background: #9fc }
        .peach { background: #fc9 }
        
        .text.mauve { background: #edf }
        .text.mint  { background: #dfe }
        .text.peach { background: #fed }
        &#13;
        <div id="myGroup" class="container">
          <div class="panel">
        
            <div class="row mint" role="button" data-toggle="collapse" data-parent="#myGroup" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
              <div class="col-xs-4">left</div>
              <div class="col-xs-4 text-center">center</div>
              <div class="col-xs-4 text-right">right</div>
            </div>
            <div id="collapseOne" class="row collapse in">
              <div class="text mint">text</div>
            </div>
        
            <div class="row mauve" role="button" data-toggle="collapse" data-parent="#myGroup" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
              <div class="col-xs-4">left</div>
              <div class="col-xs-4 text-center">center</div>
              <div class="col-xs-4 text-right">right</div>
            </div>
            <div id="collapseTwo" class="row collapse">
             <div class="text mauve">text</div>
            </div>
            
            <div class="row peach" role="button" data-toggle="collapse" data-parent="#myGroup" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
              <div class="col-xs-4">left</div>
              <div class="col-xs-4 text-center">center</div>
              <div class="col-xs-4 text-right">right</div>
            </div>
            <div id="collapseThree" class="row collapse">
              <div class="text peach">text</div>
            </div>
            
          </div>
        </div>
        
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        &#13;
        &#13;
        &#13;