Twitter bootstrap: adding a class to the open accordion title

前端 未结 11 750
-上瘾入骨i
-上瘾入骨i 2020-12-08 15:58

I am a jquery/javascript newbie. What I want to do is add a class to the open accordion title, and remove it when i open another.

heres the code:

&l         


        
相关标签:
11条回答
  • 2020-12-08 16:30

    Here's my solution to this problem. Based on some of the great answers above, but adapted to work with Bootstrap 3.1.x

    $('#accordion')
      .on('show.bs.collapse', function(e) {
        $(e.target).prev('.panel-heading').addClass('active');
      })
      .on('hide.bs.collapse', function(e) {
        $(e.target).prev('.panel-heading').removeClass('active');
      });
    
    0 讨论(0)
  • 2020-12-08 16:32

    class active dont give the item becouse in less files this class changed you have to give it css instead class. this code is work best and you can add another css that you required

     $(function () {
                $('#accordion')
         .on('show.bs.collapse', function (e) {
             $(e.target).prev('.panel-heading').css({'background-color': 'red','color':'white'})
         })
         .on('hide.bs.collapse', function (e) {
             $(e.target).prev('.panel-heading').css({ 'background-color': '#b6ff00', 'color': 'black' })
         });
            });
    
    0 讨论(0)
  • 2020-12-08 16:35

    I think it would work if you put this on your javascript:

    $('.accordion-toggle').on('shown', function () {
     $(this).addClass('active') });
    
    $('.accordion-toggle').on('hidden', function () {
     $(this).removeClass('active') });
    
    0 讨论(0)
  • 2020-12-08 16:36

    This works for me using Bootstrap 3.3.6,

     $('#accordion')
       .on('show.bs.collapse', function (e) {
           $(e.target).parent('.panel').addClass('panel-primary');
       })
       .on('hide.bs.collapse', function (e) {
           $(e.target).parent('.panel').removeClass('panel-primary');
       });
            });
    
    0 讨论(0)
  • 2020-12-08 16:38

    simply check for the collapsed class added by the BS Code:

    $('a.accordion-toggle').on('click', function () {
        if( $(this).hasClass('collapsed') !== true ){
            $(this).removeClass('active');
        }else{
            $(this).addClass('active');
        }
    });
    
    0 讨论(0)
  • 2020-12-08 16:45

    This works for me..

    $('.panel-default').on('show.bs.collapse', function () {
         $('.panel-heading').addClass('active');
    });
    
    $('.panel-default').on('hide.bs.collapse', function () {
         $('.panel-heading').removeClass('active');
    });
    
    0 讨论(0)
提交回复
热议问题