Element.hasAttribute()
El método Elemento.hasAttribute()
devuelve un valor Booleano indicando si el elemento tiene el atributo especificado o no.
Sintaxis
js
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
js
var foo = document.getElementById("foo");
if (foo.hasAttribute("bar")) {
// hacer algo
}
Polyfill
js
(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 (en-US) |
setAttributeNodeNS (en-US) |
getAttribute (DOM 1) |
getAttributeNS (en-US) |
getAttributeNode (en-US) |
getAttributeNodeNS |
hasAttribute (DOM 2) |
hasAttributeNS (en-US) |
- | - |
removeAttribute (DOM 1) |
removeAttributeNS (en-US) |
removeAttributeNode (en-US) |
- |
Especificaciones
Specification |
---|
DOM Standard # ref-for-dom-element-hasattribute① |
Compatibilidad con navegadores
BCD tables only load in the browser