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
|
---
title: ParentNode.firstElementChild
slug: orphaned/Web/API/ParentNode/firstElementChild
translation_of: Web/API/ParentNode/firstElementChild
original_slug: Web/API/ParentNode/firstElementChild
---
<p>{{ APIRef("DOM") }}</p>
<p><strong><code>ParentNode.firstElementChild</code></strong> 介面會會返回 <code>ParentNode</code>的第一個子元素(唯讀) ,如果該節點沒有子節點則返回<code>null</code>。</p>
<div class="note">
<p>This property was initially defined in the {{domxref("ElementTraversal")}} pure interface. As this interface contained two distinct set of properties, one aimed at {{domxref("Node")}} that have children, one at those that are children, they have been moved into two separate pure interfaces, {{domxref("ParentNode")}} and {{domxref("ChildNode")}}. In this case, <code>firstElementChild</code> moved to {{domxref("ParentNode")}}. This is a fairly technical change that shouldn't affect compatibility.</p>
</div>
<h2 id="Syntax_and_values" name="Syntax_and_values">語法</h2>
<pre class="syntaxbox">var <em>element</em> = node.firstElementChild;
</pre>
<h2 id="Example" name="Example">範例</h2>
<pre class="brush: html"><ul id="foo">
<li>First (1)</li>
<li>Second (2)</li>
<li>Third (3)</li>
</ul>
<script>
var foo = document.getElementById('foo');
// yields: First (1)
console.log(foo.firstElementChild.textContent);
</script>
</pre>
<h2 id="適用於IE8_IE9_和_Safari的Polyfill">適用於IE8, IE9 和 Safari的Polyfill</h2>
<pre class="brush: js">// Overwrites native 'firstElementChild' prototype.
// Adds Document & DocumentFragment support for IE9 & Safari.
;(function(constructor) {
if (constructor &&
constructor.prototype &&
constructor.prototype.firstElementChild == null) {
Object.defineProperty(constructor.prototype, 'firstElementChild', {
get: function() {
var node, nodes = this.childNodes, i = 0;
while (node = nodes[i++]) {
if (node.nodeType === 1) {
return node;
}
}
return null;
}
});
}
})(window.Node || window.Element);
</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-parentnode-firstelementchild', 'ParentNode.firstElementChild')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td>Split the <code>ElementTraversal</code> interface in {{domxref("ChildNode")}} and <code>ParentNode</code>. This method is now defined on the latter.<br>
The {{domxref("Document")}} and {{domxref("DocumentFragment")}} implemented the new interfaces.</td>
</tr>
<tr>
<td>{{SpecName('Element Traversal', '#attribute-firstElementChild', 'ElementTraversal.firstElementChild')}}</td>
<td>{{Spec2('Element Traversal')}}</td>
<td>Added its initial definition to the <code>ElementTraversal</code> pure interface and use it on {{domxref("Element")}}.</td>
</tr>
</tbody>
</table>
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
<p>{{Compat("api.ParentNode.firstElementChild")}}</p>
<h2 id="參見">參見</h2>
<ul>
<li>{{domxref("NonDocumentTypeChildNode.nextElementSibling")}}</li>
<li>{{domxref("ParentNode.lastElementChild")}}</li>
<li>The {{domxref("ParentNode")}} 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>
|