Metodo XML DOM hasAttribute()

Definizione e uso

Se l'elemento nodale corrente ha un attributo con il nome specificato, allora hasAttribute() Il metodo restituisce true, altrimenti restituisce false.

Sintassi

hasAttribute(name)
Parametro Descrizione
name Obbligatorio. Specifica l'attributo da cercare.

Esempio

Il codice seguente carica "books.xml" in xmlDoc e verifica se l'elemento <book> ha qualsiasi attributo "category":

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];
    document.getElementById("demo").innerHTML =
    x.hasAttribute("category");
{}

Prova direttamente