Phương pháp XML DOM substringData()

Định nghĩa và cách sử dụng

substringData() Phương pháp lấy dữ liệu từ phần tử văn bản.

Cú pháp

substringData(start,length)
Tham số Mô tả
start Bắt buộc. Định nghĩa từ nơi bắt đầu lấy ký tự. Giá trị bắt đầu từ số 0.
length Bắt buộc. Định nghĩa số ký tự cần lấy ra.

Mô hình

Dưới đây là mã nguồn sẽ "books.xml" tải vào xmlDoc và lấy đoạn văn bản từ phần tử <title> đầu tiên:

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;
}

Thử trực tiếp