Element.getAttributeNames()
Element.getAttributeNames()
renvoie les noms des attributs de l'élément sous la forme d'un Array
(tableau) de chaînes de caractères. Si l'élément n'a pas d'attributs, il retourne un tableau vide.
L'utilisation de getAttributeNames()
complété par getAttribute()
, est une alternative efficiente et performante pour l'accès à Element.attributes
.
Syntaxe
let attributeNames = element.getAttributeNames();
Exemple
// Itérer sur les attributs de l'élément
for(let name of element.getAttributeNames())
{
let value = element.getAttribute(name);
console.log(name, value);
}
Polyfill
if (Element.prototype.getAttributeNames == undefined) {
Element.prototype.getAttributeNames = function () {
var attributes = this.attributes;
var length = attributes.length;
var result = new Array(length);
for (var i = 0; i < length; i++) {
result[i] = attributes[i].name;
}
return result;
};
}
Spécifications
Specification |
---|
DOM Standard # ref-for-dom-element-getattributenames① |
Compatibilité des navigateurs
BCD tables only load in the browser