DOM Element对象getElementsByTagNameNS()
方法以文档顺序返回具有给定本地名称和名称空间URI的所有子元素的NodeList
。
语法
以下是getElementsByTagNameNS()
方法的使用语法。
elementObj.getElementsByTagNameNS(namespaceURI, localName)
参数
namespaceURI
- 是要查找的元素的名称空间URI。namespaceURI
- 要么是查找元素的本地名称,要么是与所有元素匹配的特殊值*
。
返回值
- 它返回一个包含所有匹配元素的新
NodeList
对象。
示例
文件: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>tanmaypatil@xyz.com</e:Email>
</Employee>
<Employee xmlns:n = "http://www.yiibai.com/non-technical/" category = "non-technical">
<n:FirstName n:lang = "en">Taniya</n:FirstName>
<n:LastName>Lee</n:LastName>
<n:ContactNo>0898-1234567890</n:ContactNo>
<n:Email>tanyia@yiibai.com</n:Email>
</Employee>
</Company>
以下示例演示了setAttributeNodeNS()
方法的用法 -
<!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");
ns="http://www.yiibai.com/technical/";
x = xmlDoc.getElementsByTagNameNS(ns, 'FirstName');
document.write(x[0].nodeName); // returns: e:FirstName
</script>
</body>
</html>
执行上面示例代码,得到下结果 -