Element:removeAttributeNode() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

Element 接口的 removeAttributeNode() 方法会从元素中移除指定的 Attr 节点。

如果您不需要在移除属性节点之前对其进行检查,则可以使用 Element.removeAttribute() 方法代替。

语法

js
removeAttributeNode(attributeNode)

参数

attributeNode

要从元素中移除的属性节点。

返回值

被移除的属性节点。

异常

NotFoundError DOMException

当元素的属性列表中不包含该属性节点时抛出。

示例

js
// Given: <div id="top" align="center" />
const d = document.getElementById("top");
const d_align = d.getAttributeNode("align");
d.removeAttributeNode(d_align);
// align is now removed: <div id="top" />

注意

如果被移除的属性具有默认值,则会被立即替换。替换的属性将具有相同的命名空间 URI 和本地名称,以及适用的原始前缀。

没有 removeAttributeNodeNS 方法;removeAttributeNode 方法可以移除带命名空间的属性和不带命名空间的属性。

规范

规范
DOM
# dom-element-removeattributenode

浏览器兼容性

另见