HTML DOM firstElementChildProperty

元素对象参考手册Element Object

Examples

Get the first child element of the <ul> element in HTML:

var x = document.getElementById("myList").firstElementChild.innerHTML;

Try it Yourself »

Definition and Usage

The firstElementChild property returns the first child element of the specified element.

The difference between the firstElementChild property and thefirstChild,property is that firstChild returns the first child node as an element node, including text nodes or comment nodes (depending on which is first), while firstElementChild returns the first child node as an element node (ignoring text and comment nodes).

This property is read-only.

Tip:Use the children property to return any child elements of the specified element.

Tip:To return the last child element of the specified element, use thelastElementChildproperty.


Browser Support

Property
firstElementChild 2.0 9.0 3.5 4.0 10.0

Syntax

element.firstElementChild

Parameters

None

Return Value

Type Description
Object Returns a Node object representing the first child element of the element, or null if there are no child elements.

Technical Details

DOM Version Core Level 3 Element Traversal

More Examples

Example

Get the tag name of the first child element of the <div> element:

var x = document.getElementById("myDIV").firstElementChild.tagName; document.getElementById("demo").innerHTML = x;

Try it Yourself »

Example

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

var x = document.getElementById("mySelect").firstElementChild.text;

Try it Yourself »


元素对象参考手册Element Object

Other Extensions