我有一个脚本,它把六个类中的一个随机添加到身体上。类编号在1到6之间,如下所示:
theme-1到theme-6。
我需要一个干净的方法把它们全部移除。现在我有一个:
$('body').removeClass('theme-1');
$('body').removeClass('theme-2');
$('body').removeClass('theme-3');
$('body').removeClass('theme-4');
$('body').removeClass('theme-5');
$('body').removeClass('theme-6');但它有点笨重,对吧?有什么方法我可以说“删除theme-1到theme-6之间的任何类”吗?
发布于 2013-02-01 16:07:51
可以使用循环索引生成类名。
classes = ""
for(i=1; i < 7; i++)
classes += 'theme-' + i + " ";
$('body').removeClass(classes);发布于 2013-02-01 16:07:52
您可以将所有内容合并成一行:
$('body').removeClass('theme-1 theme-2 theme-3 theme-4 theme-5 theme-6');或者使用一个循环,但是如果类是,则在1到6之间总是,那么这个循环就像是个人的过头了。
发布于 2013-02-01 16:07:27
你可以在一个循环中清除它们,这至少有点干净。
https://stackoverflow.com/questions/14650302
复制相似问题