1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
---
title: Element.attributes
slug: Web/API/Element/attributes
tags:
- API
- Attributi
- DOM
- Element
- Proprietà
- Referenza
translation_of: Web/API/Element/attributes
---
<p>{{ APIRef("DOM") }}</p>
<p>La proprietà <strong><code>Element.attributes</code></strong> restituisce una raccolta in tempo reale di tutti i nodi di attributo registrati nel nodo specificato. È una {{domxref("NamedNodeMap")}}, non un <code>Array</code>, 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, <code>attributes</code> è una coppia chiave/valore di stringhe che rappresenta qualsiasi informazione riguardante quell'attributo.</p>
<h2 id="Syntax" name="Syntax">Sintassi</h2>
<pre class="syntaxbox">var <em>attr</em> =<em> element</em>.attributes;
</pre>
<h2 id="Example" name="Example">Esempio</h2>
<h3 id="Esempi_basilari">Esempi basilari</h3>
<pre class="brush: js">// Ottenere il primo elemento <p> nel documento
var para = document.getElementsByTagName("p")[0];
var atts = para.attributes;</pre>
<h3 id="Enumerating_elements_attributes" name="Enumerating_elements_attributes">Enumerazione degli attributi degli elementi</h3>
<p>L'indicizzazione numerica è utile per passare attraverso tutti gli attributi di un elemento.<br>
L'esempio seguente esegue i nodi dell'attributo per l'elemento nel documento con id "paragraph" e stampa il valore di ciascun attributo.</p>
<pre class="brush: html"><!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></pre>
<h2 id="Specifiche">Specifiche</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specifica</th>
<th scope="col">Stato</th>
<th scope="col">Commento</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('DOM WHATWG', '#dom-element-attributes', 'Element.attributes')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td>Da {{SpecName('DOM3 Core')}}, spostato da {{domxref("Node")}} a {{domxref("Element")}}</td>
</tr>
<tr>
<td>{{SpecName('DOM3 Core', 'core.html#ID-84CF096', 'Element.attributes')}}</td>
<td>{{Spec2('DOM3 Core')}}</td>
<td>Nessun cambiamento da {{SpecName('DOM2 Core')}}</td>
</tr>
<tr>
<td>{{SpecName('DOM2 Core', 'core.html#ID-84CF096', 'Element.attributes')}}</td>
<td>{{Spec2('DOM2 Core')}}</td>
<td>Nessun cambiamento da {{SpecName('DOM1')}}</td>
</tr>
<tr>
<td>{{SpecName('DOM1', 'level-one-core.html#ID-84CF096', 'Element.attributes')}}</td>
<td>{{Spec2('DOM1')}}</td>
<td>Definizione iniziale.</td>
</tr>
</tbody>
</table>
<h2 id="Compatibilità_con_i_browser">Compatibilità con i browser</h2>
<div class="hidden">
<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
</div>
<p>{{Compat("api.Element.attributes")}}</p>
<h2 id="Vedi_anche">Vedi anche</h2>
<ul>
<li>{{domxref("NamedNodeMap")}}, l'interfaccia dell'oggetto restituito</li>
<li>Considerazioni sulla compatibilità con ross-browser: su <a class="external" href="http://www.quirksmode.org/dom/w3c_core.html#attributes" title="http://www.quirksmode.org/dom/w3c_core.html#attributes">quirksmode</a></li>
</ul>
|