HTML DOM Element removeAttribute() 方法

定義和用法

removeAttribute() 方法從元素中刪除屬性。

提示:刪除不存在的屬性或沒有設置但有默認值屬性的操作將被忽略。

removeAttribute() 和 removeAttributeNode() 的區別

removeAttribute() 方法刪除屬性,并且沒有返回值。

removeAttributeNode() 方法刪除 Attr 對象,并返回移除的對象。

結果將是相同的。

另請參閱:

參考手冊:

getAttribute() 方法

setAttribute() 方法

hasAttribute() 方法

hasAttributes() 方法

getAttributeNode() 方法

setAttributeNode() 方法

removeAttributeNode() 方法

教程:

HTML 屬性

實例

例子 1

從 <h1> 元素中刪除 class 屬性:

document.getElementsByTagName("H1")[0].removeAttribute("class");

親自試一試

例子 2

從 <a> 元素中刪除 href 屬性:

document.getElementById("myAnchor").removeAttribute("href");

親自試一試

語法

element.removeAttribute(name)

參數

參數 描述
name 必需。屬性的名稱。

返回值

無。

拋出

如果元素是只讀的,且不允許刪除其屬性,則該方法將拋出代碼為 NO_MODIFICATION_ALLOWED_ERR 的 DOMException 異常。

瀏覽器支持

element.removeAttribute() 是 DOM Level 1 (1998) 特性。

所有瀏覽器都完全支持它:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
支持 9-11 支持 支持 支持 支持