Metodo hasAttributes() del XML DOM

Definizione e uso

Se il nodo specificato ha qualsiasi attributo, allora hasAttributes() Il metodo restituisce true, altrimenti restituisce false.

Attenzione:Se il nodo specificato non è un nodo Elemento, il valore di ritorno è sempre false.

Sintassi

nodeObject.hasAttributes()

Parametro

Nessuno.

Dettagli tecnici

Versione DOM: Oggetto Node livello di base 2
Valore di ritorno: Valore booleano. Restituisce true se il nodo specificato ha qualsiasi attributo, altrimenti restituisce false.

Esempio

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

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.hasAttributes();
{}

亲自试一试

浏览器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持

所有主流浏览器都支持 hasAttributes() 方法。

注释:Internet Explorer 9 及更早版本不支持此方法。