blob: 96908271049a37aa046c34693779b2cffbbcb1db (
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
---
title: Element.children
slug: Web/API/Element/children
translation_of: Web/API/Element/children
tags:
- API
- DOM
- Element
- HTMLCollection
- Property
- children
browser-compat: api.Element.children
---
<p>{{ APIRef("DOM") }}</p>
<p><code><strong>Element.children </strong></code>是一个只读属性,返回 一个Node的子{{domxref("Element","elements")}} ,是一个动态更新的 {{domxref("HTMLCollection")}}。</p>
<h2 id="Syntax_and_values" name="Syntax_and_values">语法</h2>
<pre>var <em>children</em> = <em>node</em>.children;</pre>
<pre class="eval">var <em>elList</em> = elementNodeReference.children;
</pre>
<h2 id="Notes" name="Notes">备注</h2>
<p><code>children</code> 属性为只读属性,对象类型为 {{ domxref("HTMLCollection") }},你可以使用 <code>elementNodeReference.children[1].nodeName</code> 来获取某个子元素的标签名称。</p>
<h2 id="Example" name="Example">例子</h2>
<pre class="brush: js">// parg是一个指向<p>元素的对象引用
if (parg.childElementCount)
// 检查这个<p>元素是否有子元素
// 译者注:childElementCount有兼容性问题
{
var children = parg.children;
for (var i = 0; i < children.length; i++)
{
// 通过children[i]来获取每个子元素
// 注意:List是一个live的HTMLCollection对象,在这里添加或删除parg的子元素节点,都会立即改变List的值.
};
};
</pre>
<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2>
<p> </p>
<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-Element-children', 'Element.children')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>1</td>
<td>3.5</td>
<td>9 (IE6-8 incl commend nodes)</td>
<td>10</td>
<td>4</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}<span style="font-size: 14px; line-height: 1.5;"></span></td>
</tr>
</tbody>
</table>
</div>
<p style="margin-bottom: 20px; line-height: 30px;">[1] Internet Explorer 6 - 8 支持该属性,但是可能会错误地包含注释 {{domxref("Comment")}} 节点。</p>
<h2 id="相关链接" style="margin-bottom: 20px; line-height: 30px;">相关链接</h2>
<ul>
<li>The {{domxref("Element")}} and {{domxref("ChildNode")}} pure interfaces.</li>
<li>
<div class="syntaxbox">Object types implementing this pure interface: {{domxref("Document")}}, {{domxref("Element")}}, and {{domxref("DocumentFragment")}}.</div>
</li>
</ul>
|