XML DOM getElementsByTagName() 方法
❮ 元素对象
示例
以下代码片段加载 "books.xml" 到 xmlDoc 并获取所有 <title> 元素的值
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i, attnode, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('title');
for (i = 0; i < x.length; i++) {
txt += x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
输出
Everyday Italian
Harry Potter
XQuery Kick Start
学习 XML
自己动手试一试 »
定义和用法
getElementsByTagName() 方法返回所有具有指定名称的 a 元素的 NodeList。
语法
getElementsByTagName(name)
参数 | 描述 |
---|---|
name | 要搜索的标签名称字符串。值 "*" 匹配所有标签。 |
❮ 元素对象