如何使用 jQuery/JavaScript 删除所有 CSS 类? [英] How can I remove all CSS classes using jQuery/JavaScript?
问题描述
不是为元素可能具有的每个类单独调用 $("#item").removeClass()
,而是有一个可以调用的函数来从元素中删除所有 CSS 类给定元素?
Instead of individually calling $("#item").removeClass()
for every single class an element might have, is there a single function which can be called which removes all CSS classes from the given element?
jQuery 和原始 JavaScript 都可以使用.
Both jQuery and raw JavaScript will work.
推荐答案
$("#item").removeClass();
不带参数调用 removeClass
将删除项目的所有类.
Calling removeClass
with no parameters will remove all of the item's classes.
你也可以使用(但不一定推荐.正确的方法是上面的一种):
You can also use (but it is not necessarily recommended. The correct way is the one above):
$("#item").removeAttr('class');
$("#item").attr('class', '');
$('#item')[0].className = '';
如果您没有 jQuery,那么这几乎是您唯一的选择:
If you didn't have jQuery, then this would be pretty much your only option:
document.getElementById('item').className = '';
这篇关于如何使用 jQuery/JavaScript 删除所有 CSS 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!