English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM attributes attribute

HTML DOM Element-Objekt

attributesAttributes return the active collection of all attribute nodes registered to the specified element node.

Nodes can be accessed by index, starting from 0.

Use the length property of the NamedNodeMap object to determine the number of attributes.

HTML attributes are attribute nodes, allProperties and methodsCan be used for Attribute objects.

Syntax:

element.attributes
var len = document.querySelector("img").attributes.length;
Testen Sie heraus‹/›

Browser compatibility

All browsers fully support the attribute attribute:

Attribute
attributesJaJaJaJaJa

Technische Details

Rückgabewert:NamedNodeMap-Objekt, das die Sammlung von Knotenattributen darstellt
DOM-Version:DOM-Stufe1

Mehr Beispiele

Das zweite Attribut des IMG-Elements (Index1)Name:

var x = document.querySelector("img").attributes[1].name;
Testen Sie heraus‹/›

Das zweite Attribut des IMG-Elements (Index1

var x = document.querySelector("img").attributes[1].value;
Testen Sie heraus‹/›

Durchsuchen Sie alle Attribute des IMG-Elements und geben Sie den Namen und den Wert jedes Attributes aus:

var attrList = document.querySelector("img").attributes;
var txt = "";
for (let i = 0; i < attrList.length; i++) {
txt += attrList[i].name + " = " + attrList[i].value + "<br>";
}
Testen Sie heraus‹/›

HTML DOM Element-Objekt