blob: 1ce6e5da8f635be534ba2e6c2eb9c5eff48c2320 (
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
67
68
69
70
71
72
73
|
---
title: DOMTokenList.add()
slug: Web/API/DOMTokenList/add
translation_of: Web/API/DOMTokenList/add
---
<p>{{APIRef("DOM")}}</p>
<p>{{domxref("DOMTokenList")}} 接口的 <code><strong>add()</strong></code> 方法将给定的标记添加到列表中。 </p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><var>tokenList</var>.add(<var>token1</var>[, <var>token2</var>[, ...<var>tokenN</var>]]);</pre>
<h3 id="参数">参数</h3>
<dl>
<dt>token<em>N</em></dt>
<dd>一个 {{domxref("DOMString")}},表示你要添加到列表里的标记。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p><code>undefined</code></p>
<h2 id="例子">例子</h2>
<p>在下面的例子中,我们使用 {{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;
classes.add("d");
span.textContent = classes;
</pre>
<p>结果如下:</p>
<p>{{ EmbedLiveSample('Examples', '100%', 60) }}</p>
<p>你也可以添加多个标记:</p>
<pre class="brush: js">span.classList.add("d", "e", "f");
</pre>
<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-add','add()')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("api.DOMTokenList.add")}}</p>
</div>
|