Atrybuty HTML DOM item()
Przykład
Pobierz nazwę pierwszych atrybutów elementu:
const nodeMap = document.getElementById("myDiv").attributes;
let name1 = nodeMap.item(0).name;
let name2 = nodeMap.item(1).name;
const nodeMap = document.getElementById("myDiv").attributes;
let name1 = nodeMap[0].name;
let name2 = nodeMap[1].name;
Więcej przykładów poniżej.
Definicja i użycie
Metoda item()
zwraca atrybut (według indeksu) z NamedNodeMap.
Notatka
Węzły są sortowane tak, jak pojawiają się w kodzie źródłowym. Indeks zaczyna się od 0.
Zobacz też:
Składnia
namednodemap.item(index)
or simply:
namednodemap[index]
Parametry
Parameter | Description |
index | Required. The index of the attribute node in the NamedNodeMap. |
Wartość zwrotu
Rodzaj | Opis |
Węzeł | Węzeł atrybutu o określonym indeksie. Lub null jeśli numer indeksu jest poza zakresem. |
Więcej przykładów
Zmień klasę (kolor) elementu:
document.getElementById("myDiv").attributes.item(1).value = "class2";
Zmień klasę (kolor) elementu:
document.getElementById("myDiv").attributes[1].value = "class2";
Obsługa przeglądarki
attributes.item()
jest funkcją DOM Level 1 (1998).
Jest w pełni obsługiwany we wszystkich przeglądarkach:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |