Properti textContent DOM XML

Definisi dan Penggunaan

textContent Pengaturan atau mengembalikan kandungan teks untuk node dan turunannya.

Semua node anak akan dihapus dan digantikan dengan satu node teks yang mengandung nilai properti ini saat disetel.

Garis Bacaan

nodeObject.textContent

Ahli

Contoh 1

Kod berikut akan memuat "books.xml" ke xmlDoc dan mengembalikan kandungan teks elemen <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;
{}

亲自试一试

Contoh 2

Tetapkan kandungan teks untuk node:

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');
    // Tetapkan textContent
    for(i = 0; i < x.length; i++) {
        x.item(i).textContent = "Lama";
    {}
    // 输出 textContent
    for(i = 0; i < x.length; i++) {
        txt += x.item(i).textContent + "<br>";
    {}
    document.getElementById("demo").innerHTML = txt;
{}

亲自试一试