aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/children/index.html
blob: eeab92baf5693299f34f2ded7d6a2cb6062cf637 (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
---
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是一个指向&lt;p&gt;元素的对象引用
if (parg.childElementCount)
// 检查这个&lt;p&gt;元素是否有子元素
// 译者注:childElementCount有兼容性问题
 {
   var children = parg.children;
   for (var i = 0; i &lt; 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>

{{Compat("api.Element.children")}}

<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>