XML DOM getElementsByTagNameNS() 方法

Document 对象参考手册 Document 对象

定义和用法

getElementsByTagNameNS() 方法返回带有指定名称和命名空间的所有元素的 NodeList。

语法

getElementsByTagNameNS(ns,name)

参数描述
ns字符串,规定要搜索的命名空间名称。值 "*" 匹配所有的标签。
name字符串,规定要搜索的标签名。值 "*" 匹配所有的标签。


实例

下面的代码片段使用 loadXMLDoc() 把 "books.xml" 载入 xmlDoc 中,并把带有命名空间的元素节点添加到每个 <book> 元素:

实例

xmlDoc=loadXMLDoc("books.xml");
 
x=xmlDoc.getElementsByTagName('book');
var newel,newtext;
 
for (i=0;i<x.length;i++)
{
 newel=xmlDoc.createElementNS("p","edition");
 newtext=xmlDoc.createTextNode("First");
 newel.appendChild(newtext);
 x[i].appendChild(newel);
}
 
// 输出所有 title 和 edition
y=xmlDoc.getElementsByTagName("title");
z=xmlDoc.getElementsByTagNameNS("p","edition");
for (i=0;i<y.length;i++)
{
 document.write(y[i].childNodes[0].nodeValue);
 document.write(" - ");
 document.write(z[i].childNodes[0].nodeValue);
 document.write(" edition.");
 document.write(" Namespace: ");
 document.write(z[i].namespaceURI);
 document.write("<br>");
}

尝试一下 »

Document 对象参考手册 Document 对象

0 个评论

要回复文章请先登录注册