Metodo HTMLCollection namedItem()
Esempio
Ottieni il contenuto dell'elemento P con ID "myElement":
function myFunction() {
var x = document.getElementsByTagName("P").namedItem("myElement");
alert(x.innerHTML);
}
Definizione e utilizzo
Il namedItem()
metodo restituisce l'elemento con l'ID o il nome specificato in una HTMLCollection.
È anche possibile utilizzare un metodo di abbreviazione che produrrà lo stesso risultato:
var x = document.getElementsByTagName("P")["myElement"];
Supporto browser
Method | |||||
---|---|---|---|---|---|
namedItem() | Yes | Yes | Yes | Yes | Yes |
Sintassi
HTMLCollection.namedItem(name)
O:
HTMLCollection[name]
Valori dei parametri
Parameter | Description |
---|---|
name | Required. The value of the id attribute, or name attribute, of the element you want to return. |
Valore di ritorno
Un oggetto Element , che rappresenta l'elemento con ID o nome specificato.
Restituisce null se l'elemento non esiste.
Pagine correlate
Collezione HTML: metodo item()
HTMLCollection: lunghezza della proprietà