推特 bootstrap 手风琴错误

twitter bootstrap accordion bug

我在过去的几个小时里很难弄清楚我的错误。我使用 bootstrap 手风琴和车把模板引擎 - 它可以正常使用我的数据库值。

问题是当您第一次加载页面时,所有选项卡都打开了。当我关闭并重新打开其中一个时,它们都关闭了。这是页面代码:

<div class="panel-group" style="margin: 1%" id="accordion" role="tablist" aria-multiselectable="true">
  {{# each questions }}
  <div class="panel panel-primary">
    <div class="panel-heading" role="tab" id="{{@index}}">
      <h4 class="panel-title">
        <a role="button" data-toggle="collapse" data-parent="#accordion" href="#{{ this.id }}" aria-expanded="true" aria-controls="{{ this.id }}">
          {{ this.syntax }}
        </a>
      </h4>
    </div>
    <div id="{{ this.id }}" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="{{@index}}">
      <div class="panel-body">
        {{ this.answer }}        
      </div>
    </div>
  </div>
  {{/each}}
</div>

我使用把手,所以布局如下:

<!doctype html>
<html lang='en'>
<head>
    <meta charset = 'UTF-8'>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="/client/css/Site.css">
    <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="/client/js/site.js"></script>
</head>
<body>
    <ul class="topnav">
        <li><a class="active" href="/">NANO-DEV</a><li>
        <li><a href="/questions">Questions</a></li>
        <li class="right"><a href="#about">About</a></li>
    </ul>
    {{{ body }}}
</body>
</html>

我尝试遵循这个堆栈 case 所以我将这段代码添加到我的 js 文件中,但它无济于事..

$(function() {
  var transition = false;
  var $active = true;

  $('.panel-title > a').click(function(e) {
    e.preventDefault();
  });

  $('#accordion').on('show.bs.collapse',function(){
    if($active){
        $('#accordion .in').collapse('hide');
    }
  });

  $('#accordion').on('hidden.bs.collapse',function(){
    if(transition){
        transition = false;
        $('.panel-collapse').collapse('show');
    }
  });
});

尝试仅将 .collapse class 添加到 html 中的相应元素。 此外,您不需要处理显示和隐藏事件。

面临同样的问题 - 所有部分都在 .collapse 调用中打开。 +另一个有趣的错误(如下)。

但是找到了解决方案。

一开始我有正确的 HTML 标记(并且工作正常),没有 JS。但后来我想以编程方式切换部分。当我在某些部分调用 .collapse('show') 时 - 其他部分出现不同步(有些保持打开状态),即使是手动点击也是如此。这里的技巧是使用 .collapse() 调用强制初始化插件。所以为了避免所有部分展开(并使它们可以毫无问题地以编程方式控制),您必须提供两个选项,如下所示:

$acc.find('.collapse').collapse({
    parent: $acc, // to make it work as accordion
    toggle: false // don't toggle while initialising
});

查看代码片段了解更多详情。

希望对大家有所帮助

$(function(){
 var tabIndex = 0;

 var $acc = $('#accordion');
 const tabsCount = $acc.find('.collapse').length;

 // IF SKIP JS INITIALISATION - SHOW METHOD WILL NOT CLOSE OTHER TABS (UNLESS THEY'RE ALREADY INITIALIZED WITH MANUAL CLICKS)
 $acc.find('.collapse').collapse({
  parent: $acc, // to make it work as accordion
  toggle: false // don't toggle while initializing
 });

 $('#openNextTabBtn').on('click', function(){
  $acc.find('.collapse').eq(++tabIndex % tabsCount).collapse('show');
 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div>
    <span class="btn btn-default" id="openNextTabBtn">Open Next Tab</span>
</div>

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
    <div class="panel panel-default">
        <div class="panel-heading" role="tab" id="headingOne">
            <h4 class="panel-title">
                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                    Collapsible Group Item #1
                </a>
            </h4>
        </div>
        <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
            <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" role="tab" id="headingTwo">
            <h4 class="panel-title">
                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                    Collapsible Group Item #2
                </a>
            </h4>
        </div>
        <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
            <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" role="tab" id="headingThree">
            <h4 class="panel-title">
                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                    Collapsible Group Item #3
                </a>
            </h4>
        </div>
        <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
            <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>