element.hasAttribute
La méthode Element.hasAttribute()
renvoie une valeur booléenne indiquant si l'élément courant possède l'attribut spécifié ou non.
Syntaxe
Exemple
js
var foo = document.getElementById("foo");
if (foo.hasAttribute("bar")) {
// faire quelque chose
}
Polyfill
js
(function (prototype) {
prototype.hasAttribute =
prototype.hasAttribute ||
function (name) {
return !!(this.attributes[name] && this.attributes[name].specified);
};
})(Element.prototype);
Notes
Méthodes DOM traitant des attributs sur les éléments :
DOM Level 1 (le plus courant) | DOM Level 2 | DOM Level 1 traitant directement les nœuds Attr |
DOM Level 2 traitant directement les nœuds Attr |
---|---|---|---|
setAttribute (DOM 1) |
setAttributeNS |
setAttributeNode |
setAttributeNodeNS |
getAttribute (DOM 1) |
getAttributeNS |
getAttributeNode |
getAttributeNodeNS |
hasAttribute (DOM 2) |
hasAttributeNS |
- | - |
removeAttribute (DOM 1) |
removeAttributeNS |
removeAttributeNode |
- |
Spécifications
Specification |
---|
DOM Standard # ref-for-dom-element-hasattribute① |
Compatibilité des navigateurs
BCD tables only load in the browser