NamedNodeMap: removeNamedItem() 方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

NamedNodeMap 介面的 removeNamedItem() 方法會從對映中移除與給定名稱對應的 Attr

語法

js
removeNamedItem(attrName)

引數

attrName

要從對映中移除的屬性的名稱。

返回值

被移除的 Attr

異常

NotFoundError DOMException

如果不存在具有給定名稱的屬性,則會丟擲此異常。

示例

html
<pre test="testValue"></pre>
js
const pre = document.querySelector("pre");
const attrMap = pre.attributes;

let result = `The 'test' attribute initially contains '${attrMap["test"].value}'.\n`;

result += "We remove it.\n\n";
attrMap.removeNamedItem("test");

result += attrMap.getNamedItem("test")
  ? "And 'test' still exists."
  : "And 'test' is no more to be found.";

pre.textContent = result;

規範

規範
DOM
# dom-namednodemap-removenameditem

瀏覽器相容性