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 | 要搜尋的標籤名稱字串。值 "*" 匹配所有標籤。 |
❮ 元素物件