HTMLCollection length 属性

HTMLCollection length 属性 DOM HTMLCollection


定义和用法

length 属性返回 HTMLCollection 对象中元素的数量。

该属性是只读的。

该属性在循环 HTMLCollection 对象时很有用。

语法

HTMLCollection.length

返回值

返回一个数字,表示 HTMLCollection 对象中元素的数量。



浏览器支持

属性
lengthYesYesYesYesYes

实例

实例

循环输出所有 class="myclass 的元素并修改它们的背景颜色:

varx = document.getElementsByClassName("myclass"); for(i = 0; i < x.length; i++){x.item(i).style.backgroundColor = "red"; }

尝试一下 »

相关文章

HTMLCollection: item() 方法

HTML Element: getElementsByClassName() 方法

HTML Element: getElementsByTagName() 方法



HTMLCollection length 属性 DOM HTMLCollection