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
|
---
title: Element.lastElementChild
slug: Web/API/Element/lastElementChild
translation_of: Web/API/Element/lastElementChild
tags:
- API
- DOM
- Element
- Property
browser-compat: api.Element.lastElementChild
---
<p>{{ APIRef("DOM") }}</p>
<p>只读属性 <code><strong>Element.lastElementChild</strong></code> 返回对象的最后一个子{{domxref("Element", "元素")}},如果没有子元素,则返回 <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("Element")}} and {{domxref("ChildNode")}}. In this case, <code>lastElementChild</code> moved to {{domxref("Element")}}. This is a fairly technical change that shouldn't affect compatibility.</p>
</div>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">var <em>element</em> = node.lastElementChild; </pre>
<h2 id="例子">例子</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: Third (3)
console.log(foo.lastElementChild.textContent);
</script>
</pre>
<h2 id="适用于_IE8、IE9_和_Safari_的_Polyfill">适用于 IE8、IE9 和 Safari 的 Polyfill</h2>
<pre class="brush: js">// Overwrites native 'lastElementChild' prototype.
// Adds Document & DocumentFragment support for IE9 & Safari.
// Returns array instead of HTMLCollection.
;(function(constructor) {
if (constructor &&
constructor.prototype &&
constructor.prototype.lastElementChild == null) {
Object.defineProperty(constructor.prototype, 'lastElementChild', {
get: function() {
var node, nodes = this.childNodes, i = nodes.length - 1;
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">规范</th>
<th scope="col">状态</th>
<th scope="col">备注</th>
</tr>
<tr>
<td>{{SpecName('DOM WHATWG', '#dom-Element-lastelementchild', 'Element.lastElementChild')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td>Splitted the <code>ElementTraversal</code> interface in {{domxref("ChildNode")}} and <code>Element</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-lastElementChild', 'ElementTraversal.lastElementChild')}}</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.Element.lastElementChild")}}</p>
<h2 id="参见">参见</h2>
<ul>
<li>纯接口 {{domxref("Element")}} 和 {{domxref("ChildNode")}}。</li>
<li>
<div class="syntaxbox">实现了此纯接口的对象类型:{{domxref("Document")}}、{{domxref("Element")}},和 {{domxref("DocumentFragment")}}。</div>
</li>
</ul>
|