我想添加一个类并根据div的id删除一个类

时间:2013-09-17 10:32:27

标签: jquery

我想添加一个类并根据div的id删除一个类   基本上.PMessages_toolbar_collapse所有类和.PMessages_toolbar_expandAll类应该在用户点击它时删除并添加半活动“

<div class="coll-Expa-wrapper"> 
<div class="PMessages_toolbar_collapseAll " id="collapseAllPrivateMessage">
    Collapse all 
</div>//<------when user clicks  it  class .PMessages_toolbar_collapseAll should be removed and          .semi-active class should be added

<div class="semi-active" id="expandAllPrivateMessage"
    Expand all
</div>>//<------when user clicks  it .semi-active class should be removed and         .PMessages_toolbar_expandAll class should be added



  $(document).ready(function(){
  $('.semi-active').bind('click',classtoggle)
   });

function classtoggle()
{
 var toggler=$('.semi-active').attr('id');

 if(toggler =="expandAllPrivateMessage" )
    {    

 $(this).removeClass('semi-active').addClass('PMessages_toolbar_expandAll');
 $('.PMessages_toolbar_collapseAll').removeClass('PMessages_toolbar_collapseAll').addClass('semi-      activ');
   }

else if(toggler =="collapseAllPrivateMessage" )
    {
        $(this).removeClass('semi-active').addClass('PMessages_toolbar_collapseAll');
         $('.PMessages_toolbar_expandAll').removeClass('PMessages_toolbar_expandAll').addClass('semi-active');
    }

1 个答案:

答案 0 :(得分:1)

尝试

$(document).ready(function(){
    var $divs = $('.PMessages_toolbar_collapseAll, .semi-active').on('click', function(){
        $divs.toggleClass('PMessages_toolbar_collapseAll semi-active')
    })
});

演示:Fiddle