XML DOM length 속성

정의와 사용법

길이 댓글 노드의 텍스트의 길이(문자로 계산)를 반환합니다.

속성

commentNode.length

예제

아래 코드는 "books_comment.xml" 파일을 xmlDoc에 로드하고, 첫 번째 <title> 요소에서 텍스트 노드 데이터와 길이를 가져옵니다:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books_comment.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, i, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("book")[0].childNodes;
    for (i = 0; i < x.length; i++) {
    //만注释 노드 처리
        if (x[i].nodeType == 8) {
            txt += x[i].length + "<br>";
        }
    }
    document.getElementById("demo").innerHTML = txt;
}

직접 테스트해 보세요

위의 예제에서는 루프와 if 테스트 문을 사용하여注释 노드만 처리할 수 있도록 하였습니다.注释 노드의 노드 타입은 8입니다.