iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 23
0
Modern Web

從Stack Overflow學前端系列 第 23

從StackOverflow上學CODING(23) 如何用 JS刪除所有 CSS Class

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

上一篇
從StackOverflow上學CODING(22) 使input只接收數字類型的輸入
下一篇
從StackOverflow上學CODING(24)為何 ◎ܫ◎或 ☺不能成為一個 JS變數名
系列文
從Stack Overflow學前端30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言