DOMTokenList.length

 

lengthDOMTokenList接口的一个只读属性,以整数来表示,代表着当前对象中值的个数。

语法

tokenList.length;

数值

一个 整数.

示范例子

 

在示范例子里我们编写了一个span元素,使用Element.classList去检索元素<span>拥有的类,放回一个实时的DOMTokenList集合,然后把该集合中的数值个数写进 <span>的 Node.textContent 属性中。

HTML代码:

<span class="a b c"></span>

JavaScript代码:

var span = document.querySelector("span");
var classes = span.classList;
var length = classes.length;

span.textContent = 'classList length = ' + length;

运行得出的结果如下:

规范

Specification Status Comment
DOM
length
Living Standard Initial definition

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
lengthChrome Full support 1Edge Full support 12Firefox Full support 50IE ? Opera Full support YesSafari Full support 5.1WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 50Opera Android Full support YesSafari iOS Full support 5.1Samsung Internet Android Full support Yes

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown