0

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

<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');
    }
4

1 回答 1

1

尝试

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

演示:小提琴

于 2013-09-17T10:36:26.747 回答