Proprietà length degli attributi del DOM HTML

Definizione e uso

length L'attributo restituisce il numero di nodi nel NamedNodeMap.

length Gli attributi sono di sola lettura.

Nota:Gli attributi degli elementi HTML si trovano nel NamedNodeMap.

Vedi anche:

Metodo nodemap.item()

Esempio

Esempio 1

Ottieni il numero di attributi di un elemento HTML:

let num x = document.getElementById("myButton").attributes.length;

Prova tu stesso

Esempio 2

Ottieni tutti i nomi degli attributi:

const nodeMap = document.getElementById("myButton").attributes;
let text = "";
for (let i = 0; i < nodeMap.length; i++) {
  text += nodeMap[i].name + "<br>";
}

Prova tu stesso

Esempio 3

Quanti attributi ha "myImg":

let num = document.getElementById("myImg").attributes.length;

Prova tu stesso

Esempio 4

Ottieni tutti gli attributi:

const nodeMap = document.getElementById("myImg").attributes;
let text = "";
for (let i = 0; i < nodeMap.length; i++) {
  text += nodeMap[i].name + " = " + nodeMap[i].value + "<br>";
}

Prova tu stesso

Sintassi

namednodemap.length

Dettagli tecnici

Valore di ritorno

Numero, che rappresenta il numero di nodi di attributo nel nodemap.

Supporto del browser

attributes.length È una caratteristica del DOM Level 1 (1998).

Tutti i browser lo supportano:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Supporto 9-11 Supporto Supporto Supporto Supporto