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?
Both jQuery and raw JavaScript will work.
除了個別地呼叫$("#item").removeClass()刪除每個元素有的class外,有沒有一個函式可以一口氣解決所有css class?
jQuery或原生js都可以
$("#item").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):
呼叫沒有參數的removeClass可以刪除所有的class
你也可以用以下其他的方法:
$("#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 = '';