HTMLCollection 長度
示例
文件中有多少個段落
let number = document.getElementsByTagName("p").length;
自己動手試一試 »
迴圈遍歷類名為 "myclass" 的元素並改變它們的字型大小
const collection = document.getElementsByClassName("myclass");
for (let i = 0; i < collection.length; i++) {
collection[i].style.fontSize = "24px";
}
自己動手試一試 »
描述
length
屬性返回 HTMLCollection 中元素的數量。
length
屬性是隻讀的。
當您想遍歷 HTMLCollection 時,length
屬性很有用。
HTMLCollection
HTMLCollection 是一個類陣列的 HTML 元素集合(列表)。
集合中的元素可以透過索引訪問。索引從 0 開始。
語法
HTMLCollection.length
返回值
型別 | 描述 |
Number | HTMLCollection 的元素數量。 |
瀏覽器支援
HTMLCollection.length
在所有瀏覽器中都受支援
Chrome | Edge | Firefox | Safari | Opera | IE |
是 | 是 | 是 | 是 | 是 | 是 |