JavaScript 參考手冊
HTMLCollection 是 HTML 元素的集合。
HTMLCollection 對(duì)象類似一個(gè)包含 HTML 元素的數(shù)組列表。
getElementsByTagName() 方法返回的就是一個(gè) HTMLCollection 對(duì)象。
下表列出了 HTMLCollection 對(duì)象中的屬性和方法:
屬性 / 方法 | 描述 |
---|---|
item() | 返回 HTMLCollection 中指定索引的元素。 |
length | 返回 HTMLCollection 中元素的數(shù)量。 |
namedItem() | 返回 HTMLCollection 中指定 ID 或 name 屬性的元素。 |
返回所有 p 元素的集合,該集合是一個(gè) HTMLCollection 對(duì)象:
計(jì)算文檔中 p 元素的數(shù)量:
循環(huán)輸出 HTMLCollection 對(duì)象中的所有元素: