XML DOM getNamedItem() 메서드
정의와 사용법
getNamedItem()
메서드는 특정 이름을 가진 노드(namedNodeMap에서 가져온)를 반환합니다.
문법
namedNodeMap.getNamedItem(nodeName)}}
파라미터
파라미터 | 설명 |
---|---|
nodeName | 必需. 문자열. 반환할 노드의 이름을 지정합니다. |
기술 세부 사항
DOM 버전: | Core Level 1 |
---|---|
반환 값: |
Node 객체. 지정된 이름을 가진 노드를 가집니다. 지정된 맵핑의 노드가 없으면 null을 반환합니다。 |
예제
아래 코드는 "books.xml"를 xmlDoc에 로드하고 <book> 요소를 순회하며 category 속성의 값을 인쇄합니다:
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; }
예제 2
更改属性的值:
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, attlist, att, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName("book"); // 更改 category 属性的属性值 for (i = 0; i < x.length; i++) { attlist = x.item(i).attributes; att = attlist.getNamedItem("category"); att.value = "BESTSELLER"; } // 输出所有 title 和 edition for (i = 0; i < x.length; i++) { txt += x[i].getAttribute("category") + "<br>"; } document.getElementById("demo").innerHTML = txt; }
브라우저 지원
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
지원 | 지원 | 지원 | 지원 | 지원 |
모든 주요 브라우저가 지원합니다 getNamedItem()
메서드:
주의:Internet Explorer 8 및 이전 버전은 이 메서드를 지원하지 않습니다.