El m茅todo Elemento.hasAttribute()
devuelve un valor Booleano indicando si el elemento tiene el atributo especificado o no.
Sintaxis
var resultado = elemento.hasAttribute(nombre);
resultado
- contendr谩 el valor devuelto (
true
ofalse)
. nombre
- consiste en una cadena de caracteres que representa el nombre del atributo a chequear.
Ejemplo
var foo = document.getElementById("foo");
if (foo.hasAttribute("bar")) {
// hacer algo
}
Polyfill
;(function(prototype) {
prototype.hasAttribute = prototype.hasAttribute || function(name) {
return !!(this.attributes[name] &&
this.attributes[name].specified);
}
})(Element.prototype);
Notas
M茅todos DOM que tratan con atributos de elementos:
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 |
- |
Especificaciones
Specification | Status | Comment |
---|---|---|
DOM La definici贸n de 'Element.hasAttribute()' en esta especificaci贸n. |
Living Standard | From Document Object Model (DOM) Level 3 Core Specification, moved from Node to Element |
Document Object Model (DOM) Level 3 Core Specification La definici贸n de 'Element.hasAttribute()' en esta especificaci贸n. |
Obsolete | No change from Document Object Model (DOM) Level 2 Core Specification |
Document Object Model (DOM) Level 2 Core Specification La definici贸n de 'Element.hasAttribute()' en esta especificaci贸n. |
Obsolete | Initial definition. |
Compatibilidad de navegadores
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | 8.0 | (Yes) | (Yes) |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |