--- title: ParentNode.lastElementChild slug: Web/API/ParentNode/lastElementChild tags: - API - DOM - ParentNode - Property - Reference translation_of: Web/API/ParentNode/lastElementChild ---

{{ APIRef("DOM") }}

Свойство ParentNode.lastElementChild только для чтения. Возвращает последний дочерний элемент объекта или null если нет дочерних элементов.

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, lastElementChild moved to {{domxref("ParentNode")}}. This is a fairly technical change that shouldn't affect compatibility.

Синтаксис

var element = node.lastElementChild; 

Пример

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

Polyfill for IE8, IE9 and Safari

// 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);

Спецификация

Specification Status Comment
{{SpecName('DOM WHATWG', '#dom-parentnode-lastelementchild', 'ParentNode.lastElementChild')}} {{Spec2('DOM WHATWG')}} Splitted the ElementTraversal interface in {{domxref("ChildNode")}} and ParentNode. This method is now defined on the latter.
The {{domxref("Document")}} and {{domxref("DocumentFragment")}} implemented the new interfaces.
{{SpecName('Element Traversal', '#attribute-lastElementChild', 'ElementTraversal.lastElementChild')}} {{Spec2('Element Traversal')}} Added its initial definition to the ElementTraversal pure interface and use it on {{domxref("Element")}}.

Совместимость с браузерами

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (on {{domxref("Element")}}) 1.0 {{CompatGeckoDesktop("1.9.1")}} 9.0 10.0 4.0
Support on {{domxref("Document")}} and {{domxref("DocumentFragment")}} {{experimental_inline}} 29.0 {{CompatGeckoDesktop("25.0")}} {{CompatNo}} 16.0 {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (on {{domxref("Element")}}) {{ CompatVersionUnknown() }} {{CompatGeckoMobile("1.9.1")}} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Support on {{domxref("Document")}} and {{domxref("DocumentFragment")}} {{experimental_inline}} {{CompatVersionUnknown}} {{CompatGeckoMobile("25.0")}} {{CompatNo}} 16.0 {{CompatNo}}

Так же смотрите