Element.hasAttribute()
Il metodo Element.hasAttribute()
restituisce un valore booleano che indica se l'elemento specificato ha o meno l'attributo specificato.
Sintassi
var risultato = element.hasAttribute(name);
risultato
- detiene il valore di ritorno
true
ofalse
. name
- è una stringa che rappresenta il nome dell'attributo.
Esempio
var foo = document.getElementById("foo");
if (foo.hasAttribute("bar")) {
// fare qualcosa
}
Polyfill
;(function(prototype) {
prototype.hasAttribute = prototype.hasAttribute || function(name) {
return !!(this.attributes[name] &&
this.attributes[name].specified);
}
})(Element.prototype);
Appunti
DOM methods dealing with element's attributes:
Not namespace-aware, most commonly used methods | Namespace-aware variants (DOM Level 2) | DOM Level 1 methods for dealing with Attr nodes directly (seldom used) |
DOM Level 2 namespace-aware methods for dealing with Attr nodes directly (seldom used) |
---|---|---|---|
setAttribute (DOM 1) |
setAttributeNS |
setAttributeNode |
setAttributeNodeNS |
getAttribute (DOM 1) |
getAttributeNS |
getAttributeNode |
getAttributeNodeNS |
hasAttribute (DOM 2) |
hasAttributeNS |
- | - |
removeAttribute (DOM 1) |
removeAttributeNS |
removeAttributeNode |
- |
Specifiche
Specifica | Stato | Commento |
---|---|---|
DOM The definition of 'Element.hasAttribute()' in that specification. |
Living Standard | Da Document Object Model (DOM) Level 3 Core Specification, spostato da Node a Element |
Document Object Model (DOM) Level 3 Core Specification The definition of 'Element.hasAttribute()' in that specification. |
Obsolete | Nessun cambiamento da Document Object Model (DOM) Level 2 Core Specification |
Document Object Model (DOM) Level 2 Core Specification The definition of 'Element.hasAttribute()' in that specification. |
Obsolete | Definizione iniziale. |
Compatibilità con i browser
BCD tables only load in the browser