XML DOM getElementsByTagNameNS() 方法

定義和用法

getElementsByTagNameNS() 方法返回 NodeList,包含擁有指定名稱和命名空間的所有元素。

語法

elementNode.getElementsByTagNameNS(ns,name)
參數 描述
ns 字符串,規定要搜索的命名空間。值 "*" 匹配所有標籤。
name 字符串,規定要搜索的標籤名。值 "*" 匹配所有標籤。

實例

以下代码将 "books_ns.xml" 載入 xmlDoc 中,並通過標籤名稱和命名空間獲取元素:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books_ns.xml", true);
xhttp.send();
function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagNameNS("https://www.codew3c.com/meishi/", "title");
    document.getElementById("demo").innerHTML =
    x[0].nodeName;
}

亲自试一试