XML DOM textContent 속성

정의 및 사용법

textContent 속성 설정 또는 반환 노드 및 자식 노드의 텍스트 내용.

설정될 때, 모든 자식 노드가 제거되고 이 속성 값을 포함하는 단일 텍스트 노드로 대체됩니다.

문법

nodeObject.textContent

인스턴스

예제 1

아래 코드는 "books.xml"을 xmlDoc에 로드하고 <book> 요소의 텍스트 내용을 반환합니다:

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, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book');
    for(i = 0; i < x.length; i++) {
        txt += x.item(i).textContent + "<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, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book');
    // 설정 textContent
    for(i = 0; i < x.length; i++) {
        x.item(i).textContent = "Outdated";
    }
    // 텍스트 내용 출력
    for(i = 0; i < x.length; i++) {
        txt += x.item(i).textContent + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

직접 시험해 보세요