The Wayback Machine - https://web.archive.org/web/20150906072645/https://developer.mozilla.org/es/docs/Web/API/HTMLCollection

HTMLCollection

by 1 contributor:

This translation is incomplete. Please help translate this article from English.

La interface HTMLCollection representa una colección genérica (objeto tipo arreglo) de elementos (en orden de documento) y ofrece métodos y propiedades para seleccionar de la lista.

Nota: Esta interface is llamada HTMLCollection por razones históricas (antes de DOM4. las colecciones que implementaban esta interface solo podian tener elementos HTML como items).

An HTMLCollection in the HTML DOM is live; it is automatically updated when the underlying document is changed.

Una HTMLCollection en el DOM HTML es automaticamente actualizada cuando los documentos subyacentes son cambiados.

Propiedades

HTMLCollection.length Read only
Retorna el numero de items en la colección

Métodos

HTMLCollection.item()
Returns the specific node at the given zero-based index into the list. Returns null if the index is out of range.
HTMLCollection.namedItem()
Returns the specific node whose ID or, as a fallback, name matches the string specified by name. Matching by name is only done as a last resort, only in HTML, and only if the referenced element supports the name attribute. Returns null if no node exists by the given name.

Usage in JavaScript

HTMLCollection also exposes its members directly as properties by both name and index. HTML IDs may contain : and . as valid characters, which would necessitate using bracket notation for property access. Currently HTMLCollections does not recognize purely numeric IDs, which would cause conflict with the array-style access, though HTML5 does permit these.

For example, assuming there is one <form> element in the document and its id is "myForm":

var elem1, elem2;

// document.forms is an HTMLCollection

elem1 = document.forms[0];
elem2 = document.forms.item(0);

alert(elem1 === elem2); // shows: "true"

elem1 = document.forms.myForm;
elem2 = document.forms.namedItem("myForm");

alert(elem1 === elem2); // shows: "true"

elem1 = document.forms["named.item.with.periods"];

Browser compatibility

Different browsers behave differently when there are more than one elements matching the string used as an index (or namedItem's argument). Firefox 8 behaves as specified in DOM 2 and DOM4, returning the first matching element. WebKit browsers and Internet Explorer in this case return another HTMLCollection and Opera returns a NodeList of all matching elements.

Specification

See also

Etiquetas y colaboradores del documento

Contributors to this page: djrm
Última actualización por: djrm,
Ocultar la barra lateral