如何在分配了多个类的元素上切换特定类?

我有一个具有多个类的元素。 我想在单击它时切换其中一个类。

 

当我点击这个时,我需要将icon-expand更改为icon-reduce ,然后保持其他类不变。

当我使用$(this).toggleClass('icon-reduce')时,它只是在最后添加了类。 当我这样做时也是如此: $(this).toggleClass('icon-expand','icon-reduce')

这是完整的代码:

 $(function(){ $('#toggle-site-content-width').click(function(){ // on click $('#site-content').children('div').toggleClass('content-align-center'); // remove this class to expand the view to full available window width $(this).toggleClass('toggle-expand','icon-reduce'); // change the icon $(this).trigger('resize'); // trigger the window to rearrange masonry bricks }); }); 

你需要改变:

 $(this).toggleClass('toggle-expand','icon-reduce'); 

至:

 $(this).toggleClass('toggle-expand icon-reduce'); 

您不需要在此处分隔您的课程。