HTML DOM Element removeAttributeNode() 方法

定义和用法

removeAttributeNode() 方法从元素中删除属性,并返回 Attribute 对象

removeAttribute() 和 removeAttributeNode() 的区别

removeAttribute() 方法删除属性,并且没有返回值。

removeAttributeNode() 方法删除 Attr 对象,并返回移除的对象。

结果将是相同的。

替代方案:

使用 removeAttribute() 方法 更容易。

另请参阅:

参考手册:

getAttributeNode() 方法

setAttributeNode() 方法

createAttribute() 方法

getAttribute() 方法

setAttribute() 方法

hasAttribute() 方法

Attribute value 属性

HTML DOM 屬性對象

教程:

Atribut 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 支持 支持 支持 支持