XML DOM substringData() 方法

定義和用法

substringData() 方法從文本節點中獲取數據。

語法

substringData(start,length)
參數 描述
start 必需。規定從何處開始提取字符。起始值從零開始。
length 必需。規定要提取的字符數。

實例

下面的代碼將 "books.xml" 加載到 xmlDoc 中,并從第一個 <title> 元素的文本節點中獲取片段:

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 xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];
    var y = x.substringData(9, 7);
    document.getElementById("demo").innerHTML =
    x.nodeValue + "<br>" + y;
}

親自試一試