NamedNodeMap:setNamedItemNS() 方法
NamedNodeMap 接口的 setNamedItemNS() 方法将地图中由其名称标识的 Attr 放入其中。如果地图中已存在同名 Attr,则会被替换。
注意:此方法是 setNamedItem() 的别名,可以互换使用。
语法
js
setNamedItemNS(attr)
参数
attr-
要插入到地图中的属性。
返回值
如果属性被替换,则返回旧属性;如果属性是新的,则返回 null。
异常
InUseAttributeErrorDOMException-
如果属性仍是另一个地图的一部分,则抛出此异常。
示例
html
<span ob:one="one"></span>
<pre></pre>
js
const parser = new DOMParser();
// ob:one in <span> is not in a namespace, while ob:one in <warning>, is.
const xmlString =
'<warning ob:one="test" xmlns:ob="http://www.example.com/ob">Beware!</warning>';
const doc = parser.parseFromString(xmlString, "application/xml");
const span = document.querySelector("span");
const pre = document.querySelector("pre");
const warning = doc.querySelector("warning");
const attrMap = span.attributes;
let result = `The '<span>' element initially contains ${attrMap.length} attribute.\n\n`;
result += "We remove `one` from '<span>' and adds it to '<pre>'.\n";
const one = warning.attributes.removeNamedItemNS(
"http://www.example.com/ob",
"one",
);
attrMap.setNamedItemNS(one);
result += `The '<span>' element now contains ${span.attributes.length} attributes:\n\n`;
result += "Prefix\tLocal name\tQualified name\n";
result += "=========================================\n";
for (const attr of attrMap) {
result += `${attr.prefix}\t${attr.localName}\t\t${attr.name}\n`;
}
pre.textContent = result;
规范
| 规范 |
|---|
| DOM # dom-namednodemap-setnameditemns |
浏览器兼容性
加载中…