blob: 5015d41a0d9d4d5cb26a42c8a7592c3f3694f306 (
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
|
---
title: HTMLInputElement.labels
slug: Web/API/HTMLInputElement/labels
translation_of: Web/API/HTMLInputElement/labels
---
<div>{{APIRef("DOM")}}</div>
<p> <code><strong>HTMLInputElement.labels</strong></code> 为只读属性,它返回一个{{domxref("NodeList")}} 实例,绑定当前的{{HTMLElement("input")}} 节点的{{HTMLElement("label")}} 元素。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">var <var>labelElements</var> = <var>input</var>.labels;
</pre>
<h3 id="Return_value">Return value</h3>
<p> {{domxref("NodeList")}} 包含 <code><label></code> 元素和 <code><input></code> 元素.</p>
<h2 id="实例">实例</h2>
<h3 id="HTML">HTML</h3>
<pre class="brush: html"><label id="label1" for="test">Label 1</label>
<input id="test"/>
<label id="label2" for="test">Label 2</label>
</pre>
<h3 id="JavaScript">JavaScript</h3>
<pre class="brush: js">window.addEventListener("DOMContentLoaded", function() {
const input = document.getElementById("test");
for(var i = 0; i < input.labels.length; i++) {
console.log(input.labels[i].textContent); // "Label 1" and "Label 2"
}
});</pre>
<p>{{EmbedLiveSample("Example", "100%", 30)}}</p>
<h2 id="规范">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("HTML WHATWG", "forms.html#dom-lfe-labels", "labels")}}</td>
<td>{{Spec2("HTML WHATWG")}}</td>
<td>No change</td>
</tr>
<tr>
<td>{{SpecName("HTML5 W3C", "forms.html#dom-lfe-labels", "labels")}}</td>
<td>{{Spec2("HTML5 W3C")}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容">浏览器兼容</h2>
<div>
<div class="hidden">本页兼容表是由结构化数据生成的。如果你乐意为数据做贡献,请点击<a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 给我们发送请求。</div>
<p>{{Compat("api.HTMLInputElement.labels")}}</p>
</div>
|