HTML DOM Element attributes 屬性
- 上一頁 appendChild()
- 下一頁 blur()
- 返回上一層 HTML DOM Elements 對象
定義和用法
attributes
屬性返回元素中的屬性集合。
attributes
屬性返回 NamedNodeMap。
NamedNodeMap
NamedNodeMap 是元素屬性的類似數組的無序集合。
換句話說:NamedNodeMap 是 Attr 對象的列表。
NamedNodeMap 提供可返回節點數的 length 屬性。
可以通過名稱或索引號(下標)訪問節點。索引從 0 開始。
另請參閱:
實例
例子 1
<img> 元素有多少個屬性:
let numb = document.getElementById("myImg").attributes.length;
例子 2
顯示 <img> 元素的所有屬性:
const nodeMap = document.getElementById("myImg").attributes; let text = ""; for (let i = 0; i < nodeMap.length; i++) { text += nodeMap[i].name + " = " + nodeMap[i].value + "<br>"; } document.getElementById("demo").innerHTML = text;
例子 3
<button> 元素有多少個屬性:
let numb = document.getElementById("myButton").attributes.length;
例子 4
Get the name of a <button> element's second (index 1) attribute:
let attr = document.getElementById("myBtn").attributes[1].name;
語法
node.attributes
返回值
類型 | 描述 |
---|---|
NamedNodeMap | 屬性對象的集合。 |
瀏覽器支持
element.attributes
是 DOM Level 1 (1998) 特性。
所有瀏覽器都完全支持它:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
支持 | 9-11 | 支持 | 支持 | 支持 | 支持 |
- 上一頁 appendChild()
- 下一頁 blur()
- 返回上一層 HTML DOM Elements 對象