Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

javascript - 如何使用jQuery / JavaScript删除所有CSS类?(How to remove all CSS classes using jQuery/JavaScript?)

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?

(而不是为元素可能拥有的每个单独单独调用$("#item").removeClass() ,是否有一个可以调用的函数从给定元素中删除所有CSS类?)

Both jQuery and raw JavaScript will work.

(jQuery和原始JavaScript都可以工作。)

  ask by Click Upvote translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
$("#item").removeClass();

Calling removeClass with no parameters will remove all of the item's classes.

(调用不带参数的removeClass将删除所有项的类。)


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 = '';

If you didn't have jQuery, then this would be pretty much your only option:

(如果你没有jQuery,那么这将是你唯一的选择:)

document.getElementById('item').className = '';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...