我有一个具有多个类的元素。 我想在单击它时切换其中一个类。
当我点击这个时,我需要将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');
您不需要在此处分隔您的课程。
以上就是jQuery教程分享如何在分配了多个类的元素上切换特定类?相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/jquerytutorial/547710.html