XML DOM hasAttributes() Method

Definition and Usage

If the specified node has any attributes, then hasAttributes() The method returns true, otherwise returns false.

Note:If the specified node is not an Element node, the return value is always false.

Syntax

nodeObject.hasAttributes()

Parameters

None.

Technical Details

DOM Version: Core Level 2 Node Object
Return value: Boolean value. Returns true if the specified node has any attributes, otherwise returns false.

Παράδειγμα

Η παρακάτω κώδικας θα φορτώσει το "books.xml" στο xmlDoc και θα επιστρέψει αν το πρώτο <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 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 及更早版本不支持此方法。