“手动”折叠选项卡不会更改启动按钮上的文本

时间:2017-07-03 09:00:57

标签: javascript jquery twitter-bootstrap bootstrap-accordion

仅当单击按钮#collapse_init时,我才能根据active布尔变量

将按钮上的文字更改为“全部显示”或“全部隐藏”

当我手动隐藏/折叠标签(逐个)时,按钮上的文字没有改变,我想弄清楚如何解决这个问题。

  • 我的意思是,当显示或不显示所有标签时(但不通过按钮) - 按钮文字不会改变。

**目前笔无法正常工作 - 按钮无法正常工作(仅在“笔”中,在我的代码中工作),它不会一次性崩溃/隐藏(试图找出原因)

var active = false;
$('#collapse_init').click(function () {               
  if (active) {
    active = false;
    $('.panel-collapse').collapse('hide');
    $('.panel-title').attr('data-toggle', 'collapse');
    $(this).text('Show all');
  } else {
    active = true;
    $('.panel-collapse').collapse('show');
    $('.panel-title').attr('data-toggle', '');
    $(this).text('Hide all');                    
  }
});

$('.accordion-toggle').click(function () {
  var visible = $('.accordion-toggle:visible').length; 
  if(visible >0){
     $('#collapse_init').text('Hide all');
  }else{
     $('#collapse_init').text('Show all');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
   <button id="collapse_init" class="btn btn-default ">Show all</button>
	<hr>
  <div class="panel-group" id="accordion">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a class="accordion-toggle" 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">
          ONe 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 class="accordion-toggle" 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">
          Two 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 class="accordion-toggle" 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">
          Three 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>

CodePen

codepen

2 个答案:

答案 0 :(得分:2)

添加此代码并尝试

$('.accordion-toggle').click(function () {
  var visible = $('.accordion-toggle:visible').length; 
  if(visible >0){
     $('#collapse_init').text('Hide all');
  }else{
     $('#collapse_init').text('Show all');
  }
});

答案 1 :(得分:1)

我认为您遇到的问题是在面板打开/关闭之前检索到$('.accordion-toggle:visible').length

您应该使用 shown.bs.collapse hidden.bs.collapse 来检测面板何时完成打开或关闭。

这是我的破解。它不是那里,但它更清洁(也许)。

var active = false,
$collapseBtn = $('#collapse_init'),
$accordion = $('#accordion');

$collapseBtn.on('click',function(){
  active = ! active;
  $('.panel-collapse').collapse( active ? 'show' : 'hide');
  $('.panel-title').attr('data-toggle', active ? '' : 'collapse' );
})

$accordion.on({
  'shown.bs.collapse': function () {
    active = true;
    $collapseBtn.text( 'Hide all' );
  },
  'hidden.bs.collapse': function ( e) {
    active = ! ! $('.panel-collapse:visible').length;
    $collapseBtn.text( ( active ? 'Hide' : 'Show' ) + ' all' );
  }
});

https://codepen.io/jamespoel/pen/dRePNQ