HTMLCollection lengthProperty

DOM HTMLCollection 对象参考手册 DOM HTMLCollection


Definition and Usage

The length property returns the number of elements in an HTMLCollection object.

This property is read-only.

This property is useful when looping through an HTMLCollection object.

Syntax

HTMLCollection.length

Return Value

Returns a number representing the number of elements in the HTMLCollection object.



Browser Support

Property
length Yes Yes Yes Yes Yes

Examples

Examples

Loop through all elements with class="myclass" and modify their background colors:

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

Try it Yourself »

Related Articles

HTMLCollection: item()Method

HTML Element: getElementsByClassName()Method

HTML Element: getElementsByTagName()Method



DOM HTMLCollection 对象参考手册 DOM HTMLCollection

Other Extensions