此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Element:removeAttributeNode() 方法

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

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

如果你不想在移除属性节点之前检查它是否存在,那么可以改用 Element.removeAttribute() 方法。

语法

js
removeAttributeNode(attributeNode)

参数

attributeNode

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

返回值

移除的属性节点。

异常

NotFoundError DOMException

若元素的属性列表中不包含指定属性节点,则抛出该异常。

示例

js
// 给定:<div id="top" align="center" />
const d = document.getElementById("top");
const d_align = d.getAttributeNode("align");
d.removeAttributeNode(d_align);
// 现在 align 已被删除:<div id="top" />

备注

如果删除有默认值的属性,则会立刻用具有默认值的属性替换。在适用时,替换的属性具有相同的命名空间 URI、本地名称以及原始前缀。

并没有 removeAttributeNodeNS 方法;removeAttributeNode 方法可以删除命名空间限定属性和非命名空间限定属性。

规范

规范
DOM
# dom-element-removeattributenode

浏览器兼容性

参见