引导3活动类添加/移除手风琴容器外的按钮

Bootstrap 3 active class add/remove on button outside of accordion container

本文关键字:手风琴 按钮 活动 添加 引导      更新时间:2023-09-26

我看了看周围,看到很多关于添加"active"类到触发手风琴的标题。但是我不知道如何将它添加到手风琴容器外的按钮。我读了关于引导3按钮切换(http://getbootstrap.com/javascript/#buttons)。这有点工作,但活动类不被删除时,点击另一个按钮。

这是我的HTML:
<div class="btn-toolbar" role="toolbar">
              <div class="btn-group">
                  <a class="btn btn-default" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">1</a>
                  <a class="btn btn-default" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">2</a>
                  <a class="btn btn-default" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">3</a>
              </div>
              </div><!-- /.btn-toolbar -->
<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a 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">
        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 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">
        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 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">
        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>

也有一个JSfiddle设置在:http://jsfiddle.net/Bootstrap714/9Ljxbb2p/1/

我觉得这和"show .bs"有关。" Collapse "answers"hidden.bs.collapse"

Dan的第一个解决方案是一半正确的,因为当折叠折叠按钮时,按钮仍然是活动的(即单击按钮两次),不应该是这种情况。我添加了一个if语句,使按钮在折叠时处于活动状态。

$('.btn').click(function(){
    $('.btn').removeClass('active');
    var collapseId = $(this).attr("href");
    if(!$(collapseId).hasClass("in")) { // the bootstrap class "in" when the collapse is expanded
        $(this).addClass('active');
    }
})

注意它是

if(!$(collapseId).hasClass("in"))

因为这是在折叠已经展开时执行的

可以使用show .bs。折叠,如文档中所示:

$('#myCollapsible').on('hidden.bs.collapse', function () {
  // do something…
})

http://getbootstrap.com/javascript/崩溃

也许你也可以查看data-parent="#selector"部分设置另一个元素为活动

按钮实际上是关于别的东西,创建一个加载文本和禁用按钮点击。

比我原先预期的要复杂一些,但是您可以添加以下jQuery:

$('.btn').click(function(){
    active_change($(this));
});
$('.collapse-link').click(function(){
    var item = $('.collapse-' + $(this).attr("data-id"));
    active_change(item);
});
//the below activity is shared so it can be done in a single function, called from either  click above.  
function active_change(item){ 
    if($(item).hasClass('active')){
        $(item).removeClass('active');
    }else{   
        $('.btn').removeClass('active');
        $(item).addClass('active');
    }
}

并在html中添加适当的classdata-id属性。我所做的更改是,将面板手风琴链接更改为:

<a class="collapse-link" data-id="1" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">

在数字链接中增加了collapse-1collapse-2等类。

更新的jsFiddle Demo