XML DOM attribute aiki

tunawa da amfani

attribute aiki ya ci NamedNodeMap (listan aiki), wanda ya ƙunshi aiki na node na zai kai tsaye.

idan node na zai kai tsaye ba shi element, wannan aiki ya ci NULL.

sabunta:wannan aiki kawai yana da amfani ne da node na element.

kalaman samar

elementNode.attributes

wurin samar

tasiyarin da akan zai kara "books.xml" zuwa xmlDoc, kuma sami yawan aiki na farko <title> element a cikin "books.xml":

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("book")[0].attributes;
    document.getElementById("demo").innerHTML =
    x.length;
{}

亲自试一试

wurin samar

2 tasiyarin da akan zai kara "books.xml" zuwa xmlDoc, kuma sami aiki "category" na farko <book> element:
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, att, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book');
    for (i = 0; i < x.length; i++) {
        att = x.item(i).attributes.getNamedItem("category");
        txt += att.value + "<br>";
    {}
    document.getElementById("demo").innerHTML = txt;
{}

亲自试一试