blob: 92d7da9f16205c81a2dd21da52b533f4f4c41344 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
---
title: DOMTokenList.length
slug: Web/API/DOMTokenList/length
translation_of: Web/API/DOMTokenList/length
---
<p>{{APIRef("DOM")}}</p>
<p> </p>
<p><strong><code>length</code></strong>是<strong><code>DOMTokenList</code></strong>接口的一个只读属性,以整数来表示,代表着当前对象中值的个数。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">tokenList.length;</pre>
<h3 id="数值">数值</h3>
<p>一个 <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">整数</span></font>.</p>
<h2 id="示范例子">示范例子</h2>
<p> </p>
<p>在示范例子里我们编写了一个span元素,使用{{domxref("Element.classList")}}去检索元素{{htmlelement("span")}}拥有的类,放回一个实时的<code>DOMTokenList</code>集合,然后把该集合中的数值个数写进 <code><span></code>的 {{domxref("Node.textContent")}} 属性中。</p>
<p>HTML代码:</p>
<pre class="brush: html"><span class="a b c"></span></pre>
<p>JavaScript代码:</p>
<pre class="brush: js">var span = document.querySelector("span");
var classes = span.classList;
var length = classes.length;
span.textContent = 'classList length = ' + length;
</pre>
<p>运行得出的结果如下:</p>
<p>{{ EmbedLiveSample('Examples', '100%', 60) }}</p>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('DOM WHATWG','#dom-domtokenlist-length','length')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("api.DOMTokenList.length")}}</p>
</div>
|