HTML DOM Element firstElementChild 属性

定义和用法

firstElementChild 属性返回指定元素的第一个子元素。

firstElementChild 属性是只读的。

firstElementChild 属性返回与 children[0] 相同的值。

另请参阅:

children 属性

childElementCount 属性

lastElementChild 属性

tagName Property

childNodes Property

Example

Example 1

Get the HTML content of the first child element:

let text = element.firstElementChild.innerHTML;

Try It Yourself

Example 2

Get the tag name of the first child element of "myDIV":

let text = document.getElementById("myDIV").firstElementChild.tagName;

Try It Yourself

Example 3

Get the text of the first child element of the <select> element:

let text = document.getElementById("mySelect").firstElementChild.text;

Try It Yourself

HTML Nodes and Elements

In HTML DOMIn the (Document Object Model), an HTML document is a collection of nodes that have (or do not have) child nodes.

NodeRefers to element nodes, text nodes, and comment nodes.

ElementThe spaces between them are also text nodes.

And elements are just element nodes.

Child Nodes and Child Elements

childNodes ReturnsChild NodesElement nodes, text nodes, and comment nodes.

children ReturnsChild ElementsInstead of text and comment nodes.

firstChild and firstElementChild

firstChild Returns the first oneChild NodesElement nodes, text nodes, or comment nodes. The spaces between elements are also text nodes.

firstElementChild Returns the first oneChild ElementsDoes not return text nodes and comment nodes.

lastChild and lastElementChild

lastChild Returns the last oneChild NodesElement nodes, text nodes, or comment nodes. The spaces between elements are also text nodes.

lastElementChild Returns the last oneChild ElementsDoes not return text nodes and comment nodes.

Syntax

element.firstElementChild

Return Value

Type Description
Node The first child element of the element. Returns null if there are no children.

Browser Support

element.firstElementChild It is a DOM Level 3 (2004) feature.

All browsers fully support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support