HTML DOM Attribuisce la lunghezza
Esempio
Ottieni il numero di attributi di un elemento HTML:
let num x = document.getElementById("myButton").attributes.length;
Altri esempi di seguito.
Definizione e utilizzo
La length
proprietà restituisce il numero di nodi in una NamedNodeMap.
La length
proprietà è di sola lettura.
Nota
Gli attributi di un elemento HTML si trovano in una NamedNodeMap.
Guarda anche:
Sintassi
namednodemap.length
Dettagli tecnici
Valore di ritorno: | Un numero, che rappresenta il numero di nodi di attributo nella mappa dei nodi |
---|
Altri esempi
Ottieni il nome di tutti gli attributi:
const nodeMap = document.getElementById("myButton").attributes;
let text = "";
for (let i = 0; i < nodeMap.length; i++) {
text += nodeMap[i].name + "<br>";
}
Quanti attributi ha "myImg":
let num = document.getElementById("myImg").attributes.length;
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>";
}
Supporto browser
attributes.length
è una funzionalità DOM di livello 1 (1998).
È completamente supportato in tutti i browser:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |