HTML DOM Element removeAttributeNode() 方法

定義和用法

removeAttributeNode() 方法從元素中刪除屬性,并返回 Attribute 對象

removeAttribute() 和 removeAttributeNode() 的區別

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

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

結果將是相同的。

替代方案:

使用 removeAttribute() 方法 更容易。

另請參閱:

參考手冊:

getAttributeNode() 方法

setAttributeNode() 方法

createAttribute() 方法

getAttribute() 方法

setAttribute() 方法

hasAttribute() 方法

Attribute value 屬性

HTML DOM Attribute 對象

教程:

HTML 屬性

實例

例子 1

從第一個 <h1> 元素中刪除 class 屬性節點:

const element = document.getElementsByTagName("H1")[0];
const attr = element.getAttributeNode("class");
element.removeAttributeNode(attr);

親自試一試

例子 2

從鏈接中刪除 href 屬性節點:

const element = document.getElementById("myAnchor");
const attr = element.getAttributeNode("href");
element.removeAttributeNode(attr);

親自試一試

語法

element.removeAttributeNode(oldAttr)

參數

參數 描述
oldAttr 必需。要刪除的屬性節點。

返回值

類型 描述
對象 表示已刪除屬性的 Attr 對象。

拋出

異常 描述
NO_MODIFICATION_ALLOWED_ERR 當前元素是只讀的,不允許刪除屬性。
NOT_FOUND_ERR oldAttr 不是當前元素的屬性。

說明

該方法將從當前元素的屬性集合中刪除(并返回)Attr 節點。如果 DTD 給刪除的屬性設置了默認值,那么該方法將添加一個新的 Attr 節點,表示這個默認值。

提示:removeAttribute() 方法 代替該方法往往會更簡單。

瀏覽器支持

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

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

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