如何添加“active”类来打开Bootstrap 3手风琴项目?

时间:2014-01-20 08:12:07

标签: jquery twitter-bootstrap-3

如何添加“active”类来打开Bootstrap 3手风琴项目?在我的示例中,id添加“active”但不会从其他元素中删除。我正在使用Bootstrap 3

<div class="panel-group accordions" id="accordion1">
    <div class="panel panel-custom">
        <div class="panel-heading">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#accordion" href="#collapse1">
                    Collapsible Group Item #1
                </a>
            </h4>
        </div>
        <div id="collapse1" class="panel-collapse collapse in">
            <div class="panel-body">
                ....
            </div>
        </div>
    </div>
    ... 
</div>


 jQuery('.accordions .panel-heading a[data-toggle="collapse"]').on('click', function () {   
    $('.accordions').find(jQuery('.panel-heading a[data-toggle="collapse"]')).removeClass('actives');
    $(this).addClass('actives');
 });

1 个答案:

答案 0 :(得分:7)

从这行代码中删除jQuery:

$('.accordions').find(jQuery('.panel-heading a[data-toggle="collapse"]')).removeClass('actives');

像这样使用:

$('.accordions').find('.panel-heading a[data-toggle="collapse"]').removeClass('actives');

你也可以这样做:

jQuery('.accordions .panel-heading a[data-toggle="collapse"]').on('click', function () {   
    jQuery('.accordions .panel-heading a[data-toggle="collapse"]').removeClass('actives');
    $(this).addClass('actives');
 });