XML DOM setAttributeNode() 方法

定义和用法

setAttributeNode() 方法添加新的属性节点。

如果元素中已存在同名的属性,则会将其替换为新属性。

如果新属性替换了现有属性,则返回被替换的属性节点,否则返回 null。

语法

elementNode.setAttributeNode(అంశాన్ని నిర్వచించుము)
) పారామీటర్స్
అంశాన్ని నిర్వచించుము అవసరము. సెట్ చేయవలసిన అంశాన్ని నిర్ణయించుము.

ప్రతిమాత్రము

ఈ కోడ్ "books.xml" ను xmlDoc లోకి లోడ్ చేయడానికి మరియు అన్ని <book> అంశాలకు "edition" అంశాన్ని జోడించడానికి ఉపయోగించబడుతుంది:

వారు xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   ఇఫ్ (ఈ.readyState == 4 && ఈ.status == 200) {
       మైఫంక్షన్(ఈ);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
ఫంక్షన్ మైఫంక్షన్(ఎక్సిల్);
    వారు x, y, z, i, newatt, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book');
    for (i = 0; i < x.length; i++) {
        newatt = xmlDoc.createAttribute("edition");
        newatt.value = "first";
        x[i].setAttributeNode(newatt);
    }
    // అన్ని "సంచిక" అంశాల విలువలను అవుట్పుట్ చేయుము Output all "edition" attribute values
    for (i = 0; i < x.length; i++) {
        txt += "Edition: " + x[i].getAttribute("edition") + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

亲自试一试