XML DOM getNamedItem() Metode
Objek BernamaNodeMap
Contoh
Fragmen kode berikut memuat " books.xml " ke dalam xmlDoc, mengulang elemen <book> dan mencetak nilai atribut kategori:
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, att, xmlDoc,
txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book');
for (i = 0; i < x.length; i++) {
att = x.item(i).attributes.getNamedItem("category");
txt += att.value + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
Output dari kode di atas akan menjadi:
cooking
children
web
web
Definisi dan Penggunaan
Metode getNamedItem() mengembalikan node dengan nama tertentu (dari NamedNodeMap).
Dukungan Peramban
Metode getNamedItem() didukung di semua browser utama.
Catatan: Internet Explorer 8 dan versi sebelumnya tidak mendukung metode ini.
Sintaksis
namedNodeMap.getNamedItem(nodeName)
Parameter
Parameter | Type | Description |
---|---|---|
nodeName | String | Required. The name of the node to return |
Nilai Kembali
Type | Description |
---|---|
Node object | The node with the specified name, , or null if it does not identify any node in the map |
Detail Teknis
Versi DOM | Tingkat Inti 1 |
---|
Demo Coba Sendiri
Objek BernamaNodeMap