概述
hasAttributeNS 返回一个布尔值,指示该元素是否包含有指定的属性(attribute)。
语法
result =element.hasAttributeNS(namespace,localName)
result为返回的布尔值:true或false。namespace是一个字符串,表示属性的命名空间。localName是一个字符串,表示属性的名称。
例子
// 在为属性设置值之前检测该属性是否存在
var d = document.getElementById("div1");
if (d.hasAttributeNS(
"http://www.mozilla.org/ns/specialspace/",
"special-align")) {
d.setAttribute("align", "center");
}
备注
该方法与hasAttribute类似,只是要检查的属性由命名空间和名称指定。只有使用命名空间的 XML 文档才使用方法
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 |
getAttributeNode |
getAttributeNodeNS |
hasAttribute (DOM 2) |
hasAttributeNS |
- | - |
removeAttribute (DOM 1) |
removeAttributeNS |
removeAttributeNode |
- |