Element.setAttributeNodeNS()
setAttributeNodeNS 可以给一个元素添加一个新的命名空间的属性节点。
(如果对中文有疑惑,请直接阅读原文)
Syntax
replacedAttr = element.setAttributeNodeNS(attributeNode)
replacedAttr是被替换的节点,如果存在,由此函数返回。attributeNode是一个属性节点。
Example
// <div id="one" xmlns:myNS="http://www.mozilla.org/ns/specialspace"
myNS:special-align="utterleft">one</div>
// <div id="two">two</div>
var myns = "http://www.mozilla.org/ns/specialspace";
var d1 = document.getElementById("one");
var d2 = document.getElementById("two");
var a = d1.getAttributeNodeNS(myns, "special-align");
d2.setAttributeNodeNS(a.cloneNode(true));
alert(d2.attributes[1].value) // returns: `utterleft'
Notes
如果指定的属性在元素上存在,接着此属性被新的属性替换的话被替换的属性会被返回。
注意:如果你尝试设置的时候没有克隆那个节点,Mozia 会抛出一个 NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR:"Attribute already in use" 错误,因为 DOM 需要克隆属性之后才能重复使用(不像其他节点一样可以被删除)。
DOM methods dealing with element's attributes:
| Not namespace-aware, most commonly used methods | Namespace-aware variants (DOM Level 2) | DOM Level 1 methods for dealing with Attr nodes directly (seldom used) |
DOM Level 2 namespace-aware methods for dealing with Attr nodes directly (seldom used) |
|---|---|---|---|
setAttribute (DOM 1) |
setAttributeNS |
setAttributeNode |
setAttributeNodeNS |
getAttribute (DOM 1) |
getAttributeNS (en-US) |
getAttributeNode |
getAttributeNodeNS |
hasAttribute (DOM 2) |
hasAttributeNS |
- | - |
removeAttribute (DOM 1) |
removeAttributeNS |
removeAttributeNode |
- |