aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/api/element/attributes/index.html
blob: 75c34e2d252ef7de661671228f893bfd995fa81f (plain)
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
---
title: Element.attributes
slug: Web/API/Element/attributes
translation_of: Web/API/Element/attributes
---
<p>{{ APIRef("DOM") }}</p>

<p>La propiedad <strong><code>Element.attributes</code></strong> retorna una colección "viva" cuyos nodos son todos los atributos registrados en el nodo especificado. Es un {{domxref("NamedNodeMap")}}, no un <code>Array</code>, así que no tiene los métodos de {{jsxref("Array")}} y los índices de nodo {{domxref("Attr")}} pueden diferir en cada navegador. Más concretamente <code>attributes</code> es un conjunto de pares de cadenas nombre/valor que representan la información relativa a cada atributo.</p>

<h2 id="Syntax" name="Syntax">Sintaxis</h2>

<pre class="syntaxbox">var <em>attr</em> =<em> element</em>.attributes;
</pre>

<h2 id="Example" name="Example">Ejemplo</h2>

<h3 id="Ejemplos_básicos">Ejemplos básicos</h3>

<pre class="brush: js">// Obtiene el primer elemento &lt;p&gt; en el documento
var para = document.getElementsByTagName("p")[0];
var atts = para.attributes;</pre>

<h3 id="Notes" name="Notes">Enumerando atributos de elemtentos</h3>

<p>La indexación numérica es útil para recorrer de todos los atributos de un elemento.<br>
 El siguiente ejemplo corre a través de los atributors del elemento con id "p1" en el documento, e imprime el valor de cada atributo.</p>

<pre class="brush: html">&lt;!DOCTYPE html&gt;

&lt;html&gt;

 &lt;head&gt;
  &lt;title&gt;Attributes example&lt;/title&gt;
  &lt;script type="text/javascript"&gt;
   function listAttributes() {
     var paragraph = document.getElementById("paragraph");
     var result = document.getElementById("result");

     // Primero, verifiquenmos que el párrafo tiene algún atributo
     if (paragraph.hasAttributes()) {
       var attrs = paragraph.attributes;
       var output = "";
       for(var i = attrs.length - 1; i &gt;= 0; i--) {
         output += attrs[i].name + "-&gt;" + attrs[i].value;
       }
       result.value = output;
     } else {
       result.value = "No hay atributos que mostrar";
     }
   }
  &lt;/script&gt;
 &lt;/head&gt;

&lt;body&gt;
 &lt;p id="paragraph" style="color: green;"&gt;Párrafo de ejemplo&lt;/p&gt;
 &lt;form action=""&gt;
  &lt;p&gt;
    &lt;input type="button" value="Muestra el nombre cada atributo y su valor"
      onclick="listAttributes();"&gt;
    &lt;input id="result" type="text" value=""&gt;
  &lt;/p&gt;
 &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>

<h2 id="Especificaciones">Especificaciones</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Especificación</th>
   <th scope="col">Estado</th>
   <th scope="col">Observaciones</th>
  </tr>
  <tr>
   <td>{{SpecName('DOM WHATWG', '#dom-element-attributes', 'Element.attributes')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>From {{SpecName('DOM3 Core')}}, moved from {{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>Sin cambios desde {{SpecName('DOM2 Core')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM2 Core', 'core.html#ID-84CF096', 'Element.attributes')}}</td>
   <td>{{Spec2('DOM2 Core')}}</td>
   <td>Sin cambios desde {{SpecName('DOM1')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM1', 'level-one-core.html#ID-84CF096', 'Element.attributes')}}</td>
   <td>{{Spec2('DOM1')}}</td>
   <td>Definición inicial.</td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilidad_con_navegadores">Compatibilidad con navegadores</h2>

{{Compat("api.Element.attributes")}}

<h2 id="Ver_también">Ver también</h2>

<ul>
 <li>{{domxref("NamedNodeMap")}}, la interfaz del objeto retornado.</li>
 <li>Consideracións sobre compatibilidad cruzada entre navegadores: en <a class="external" href="http://www.quirksmode.org/dom/w3c_core.html#attributes">quirksmode</a></li>
</ul>