I volontari di MDN non hanno ancora tradotto questo articolo in Italiano. Unisciti a noi e traducilo tu stesso.
Puoi anche consultare l’articolo in English (US).

The getAttributeNames() method of the Element interface returns the attribute names of the element as an Array of strings. If the element has no attributes it returns an empty array.

Using getAttributeNames() along with getAttribute(), is a memory-efficient and performant alternative to accessing Element.attributes.

Syntax

let attributeNames = element.getAttributeNames();

Example

// Iterate over element's attributes
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;
  };
}

Specifications

Specification Status Comment
DOM
The definition of 'Element.getAttributeNames' in that specification.
Living Standard Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
getAttributeNamesChrome Full support 61Edge Full support 18Firefox Full support 45IE No support NoOpera Full support 48Safari Full support 10WebView Android Full support 61Chrome Android Full support 61Edge Mobile ? Firefox Android Full support 45Opera Android Full support 48Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

Tag del documento e collaboratori

Hanno collaborato alla realizzazione di questa pagina: mdnwebdocs-bot, mfluehr, fscholz, mfuji09, Yaffle, jpmedley, tophf, adria
Ultima modifica di: mdnwebdocs-bot,