Element: removeAttribute() 方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

Element 方法 removeAttribute() 會從元素中移除指定名稱的屬性。

語法

js
removeAttribute(attrName)

引數

attrName

一個字串,指定要從元素中移除的屬性的名稱。如果指定的屬性不存在,removeAttribute() 會直接返回,而不會產生錯誤。

返回值

無(undefined)。

用法說明

您應該使用 removeAttribute() 來代替直接或使用 setAttribute() 將屬性值設定為 null。許多屬性在設定為 null 時將無法按預期工作。

示例

js
// Given: <div id="div1" disabled width="200px">
document.getElementById("div1").removeAttribute("disabled");
// Now: <div id="div1" width="200px">

規範

規範
DOM
# ref-for-dom-element-removeattribute①

瀏覽器相容性

另見