--- title: Element.attributes slug: Web/API/Element/attributes tags: - API - Attributi - DOM - Element - Proprietà - Referenza translation_of: Web/API/Element/attributes ---
{{ APIRef("DOM") }}
La proprietà Element.attributes
restituisce una raccolta in tempo reale di tutti i nodi di attributo registrati nel nodo specificato. È una {{domxref("NamedNodeMap")}}, non un Array
, quindi non ha metodi predefiniti degli {{jsxref("Array")}} e non ha i metodi {{domxref("Attr")}} dei nodi che possono differire tra i browser. Per essere più specifici, attributes
è una coppia chiave/valore di stringhe che rappresenta qualsiasi informazione riguardante quell'attributo.
var attr = element.attributes;
// Ottenere il primo elemento <p> nel documento var para = document.getElementsByTagName("p")[0]; var atts = para.attributes;
L'indicizzazione numerica è utile per passare attraverso tutti gli attributi di un elemento.
L'esempio seguente esegue i nodi dell'attributo per l'elemento nel documento con id "paragraph" e stampa il valore di ciascun attributo.
<!DOCTYPE html> <html> <head> <title>Attributes example</title> <script type="text/javascript"> function listAttributes() { var paragraph = document.getElementById("paragraph"); var result = document.getElementById("result"); // First, let's verify that the paragraph has some attributes if (paragraph.hasAttributes()) { var attrs = paragraph.attributes; var output = ""; for(var i = attrs.length - 1; i >= 0; i--) { output += attrs[i].name + "->" + attrs[i].value; } result.value = output; } else { result.value = "No attributes to show"; } } </script> </head> <body> <p id="paragraph" style="color: green;">Sample Paragraph</p> <form action=""> <p> <input type="button" value="Show first attribute name and value" onclick="listAttributes();"> <input id="result" type="text" value=""> </p> </form> </body> </html>
Specifica | Stato | Commento |
---|---|---|
{{SpecName('DOM WHATWG', '#dom-element-attributes', 'Element.attributes')}} | {{Spec2('DOM WHATWG')}} | Da {{SpecName('DOM3 Core')}}, spostato da {{domxref("Node")}} a {{domxref("Element")}} |
{{SpecName('DOM3 Core', 'core.html#ID-84CF096', 'Element.attributes')}} | {{Spec2('DOM3 Core')}} | Nessun cambiamento da {{SpecName('DOM2 Core')}} |
{{SpecName('DOM2 Core', 'core.html#ID-84CF096', 'Element.attributes')}} | {{Spec2('DOM2 Core')}} | Nessun cambiamento da {{SpecName('DOM1')}} |
{{SpecName('DOM1', 'level-one-core.html#ID-84CF096', 'Element.attributes')}} | {{Spec2('DOM1')}} | Definizione iniziale. |
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
{{Compat("api.Element.attributes")}}