ພາສາ HTML DOM Element hasChildNodes()

ການອະທິບາຍແລະການນໍາໃຊ້

ຖ້າມີຫົວຂໍ້ພົນມາບັນຫາຫຼັກໄດ້ມີຫົວຂໍ້ລູກໃດໜຶ່ງhasChildNodes() method returns true, otherwise return false.

hasChildNodes() The method is read-only.

Note

Whitespace between nodes is considered as child nodes (text nodes).

See also:

childNodes Property

firstChild Property

lastChild Property

nextSibling Property

previousSibling Property

parentNode Property

HTML Node and Element

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

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

ElementThe whitespace between them is also a text node.

While the element is just an element node.

Child Node and Child Element

childNodes ReturnsChild Node(element node, text node, and comment node).

children ReturnsChild Element(not text and comment nodes).

Sibling and Element Sibling

SiblingIs 'brother' and 'sister'.

SiblingAre nodes that have the same parent node (in the same childNodes List).

Element SiblingAre elements that have the same parent element (in the same children List).

Example

Example 1

Does the element have child nodes:

let answer = element.hasChildNodes();

Try It Yourself

Example 2

Remove the first child node of the element:

if (element.hasChildNodes()) {
  element.removeChild(element.childNodes[0]);
}

Try It Yourself

Syntax

element.hasChildNodes()

Parameter

None.

Return Value

Type Description
Boolean Value If the node has child nodes, it returns boolean true, otherwise false.

Browser Support

element.hasChildNodes() Is DOM Level 1 (1998) 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