TUTORIAL前端与浏览器FETCHED
HTMLCollection length 属性
HTMLCollection length 属性 DOM HTMLCollection 定义和用法 length 属性返回 HTMLCollection 对象中元素的数量。 该属性是只读的。 该属性在循环 HTMLCollection 对象时很有用。 语法 HTMLCollection.length 返回值 返回一个数字,表示 HTMLCollection 对象中元素的数量。 浏览器支持 属性 ..
更新于 2026/7/16
教程详情页默认 noindex。
HTMLCollection length 属性
定义和用法
length 属性返回 HTMLCollection 对象中元素的数量。
该属性是只读的。
该属性在循环 HTMLCollection 对象时很有用。
语法
HTMLCollection.length
返回值
返回一个数字,表示 HTMLCollection 对象中元素的数量。
浏览器支持
| 属性 | |||||
|---|---|---|---|---|---|
| length | Yes | Yes | Yes | Yes | Yes |
实例
实例
循环输出所有 class="myclass 的元素并修改它们的背景颜色:
var x = document.getElementsByClassName("myclass");
for (i = 0; i < x.length; i++) {
x.item(i).style.backgroundColor = "red";
}
尝试一下 »
相关文章
HTMLCollection: item() 方法
HTML Element: getElementsByClassName() 方法
HTML Element: getElementsByTagName() 方法
DOM HTMLCollection