getAttributeNodeNS()
方法通过本地名称和名称空间URI检索Attr
节点。
语法
以下是getAttributeNodeNS()
方法的使用语法。
elementObj.getAttributeNodeNS(namespace,nodeName)
参数
namespace
- 是一个指定属性名称空间的字符串。nodeName
- 是一个指定属性名称的字符串。
返回值
- 它返回指定属性的
Attr
节点。
示例
文件:node_ns.xml 的内容如下 -
<?xml version = "1.0"?>
<Company>
<Employee xmlns:e = "http://www.yiibai.com/technical/" category = "technical">
<e:FirstName e:lang = "en">Tianya</e:FirstName>
<e:LastName>Su</e:LastName>
<e:ContactNo>(0898)1234567890</e:ContactNo>
<e:Email>tianya.su@xyz.com</e:Email>
</Employee>
<Employee xmlns:n = "http://www.yiibai.com/non-technical/" category = "non-technical">
<n:FirstName n:lang = "en">Max</n:FirstName>
<n:LastName>Lee</n:LastName>
<n:ContactNo>1234667898</n:ContactNo>
<n:Email>max.su@xyz.com</n:Email>
</Employee>
</Company>
以下示例演示了getAttributeNodeNS()
方法的用法 -
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(filename) {
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else // code for IE5 and IE6
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
</script>
</head>
<body>
<script>
xmlDoc = loadXMLDoc("/node_ns.xml");
x = xmlDoc.getElementsByTagName("Employee")[0];
//alert(x);
ns="http://www.yiibai.com/technical/";
var attributenodens = x.getAttributeNodeNS(ns,"lang")
document.write("nodename: "+attributenodens.nodeName);
document.write("<br>nodevalue: "+attributenodens.nodeValue);
</script>
</body>
</html>
执行上面示例代码,得到以下结果 -
nodename: e:lang
nodevalue: en