XML DOM getAttributeNS() 方法

定義和用法

getAttributeNS() 方法通過命名空間 URI 和名稱來獲取屬性值。

語法:

elementNode.getAttributeNS(ns,name)
參數 描述
ns 必需。規定從中獲取屬性值的命名空間 URI。
name 必需。規定從中取得屬性值的屬性。

實例

在所有的例子中,我們將使用 XML 文件 books_ns.xml,以及 JavaScript 函數 loadXMLDoc()

下面的代碼片段從 "books_ns.xml" 中的第一個 <title> 元素中獲取 "lang" 屬性的值:

xmlDoc=loadXMLDoc("books_ns.xml");
x=xmlDoc.getElementsByTagName("title")[0];
ns="http://www.codew3c.com/children/";
document.write(x.getAttributeNS(ns,"lang"));

以上代碼的輸出:

en