English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
childNodesThe read-only property returns the collection of child nodes of the given element, with the index of the first child node as 0.
Nodes in the collection are sorted in the order they appear in the source code, and they can be accessed by index.
Use the length property to determine the number of child nodes, and then you can iterate over all child nodes and extract the required information.
Whitespace within elements is considered text, and text is considered a node. Comments are also considered nodes.
To get a collection of only elements (excluding text and comment nodes), please usechildrenProperty.
nodeIf .childNodes[0] produces the same result thenfirstChildProperty.
node.childNodes
var list = document.body.childNodes;Test and See‹/›
All browsers fully support the childNodes property:
property | |||||
childNodes | Yes | Yes | Yes | Yes | Yes |
Return Value: | A NodeList object representing a collection of nodes |
---|---|
DOM Version: | DOM Level1 |
Find out how many child nodes the DIV element has:
var len = document.querySelector("div").childNodes.length;Test and See‹/›
Change the second child node of the DIV element (index1)Background Color:
var nodes = document.querySelector("div").childNodes; nodes[1nodes[.style.backgroundColor = "coral";]Test and See‹/›
HTML DOM Reference:node .firstChild property
HTML DOM Reference:node.lastChild property
HTML DOM Reference:node.parentNode property
HTML DOM Reference:node.nextSibling property
HTML DOM Reference:node.previousSibling property
HTML DOM Reference:node.nodeName property