如何使用jQuery/JavaScript删除所有CSS类? [英] How to remove all CSS classes using jQuery/JavaScript?

查看:97
本文介绍了如何使用jQuery/JavaScript删除所有CSS类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是每个元素可能拥有的每个类都单独调用$("#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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆