twitter bootstrap手风琴bug

时间:2016-08-29 12:00:25

标签: jquery twitter-bootstrap handlebars.js bootstrap-accordion

我很难在过去几个小时内弄错...我使用自举手风琴和把手模板引擎 - 它可以正常使用我的数据库值。

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

<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');
    }
  });
});

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

但是找到了解决方案。

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

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

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

我希望它有所帮助

&#13;
&#13;
$(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');
	});
});
&#13;
<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>
&#13;
&#13;
&#13;