gpt4 book ai didi

javascript - Bootstrap 3 事件类在 Accordion 容器外的按钮上添加/删除

转载 作者:行者123 更新时间:2023-11-30 05:33:31 26 4
gpt4 key购买 nike

我环顾四周,看到很多关于将“事件”类添加到触发 Accordion 的标题的信息。但我不知道如何将它添加到 Accordion 容器外的按钮。我阅读了有关 Bootstrap 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/

我觉得它与“shown.bs.collapse”和“hidden.bs.collapse”有关

如有任何帮助,我们将不胜感激。

最佳答案

Dan 的第一个解决方案是正确的一半,因为当 Accordion 折叠时,按钮仍然处于事件状态(即单击按钮两次),但事实并非如此。我为按钮添加了一个 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"))

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

关于javascript - Bootstrap 3 事件类在 Accordion 容器外的按钮上添加/删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25343872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com