English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM hasChildNodes() method

Objeto Elemento do HTML DOM

hasChildNodes()The method returns a boolean value indicating whether the given Node has child nodes.

Note:Whitespace within the node is considered a text node, therefore, if any whitespace or newline characters are retained within the element, the element still has child nodes.

Syntax:

node.hasChildNodes()
var div = document.getElementById("myDiv");
div.hasChildNodes();
Testar e Verificar‹/›

Browser compatibility

All browsers fully support the hasChildNodes() method:

Method
hasChildNodes()IsIsIsIsIs

Technical details

Return value:Boolean value, returns true if the node has child nodes, otherwise returns false
Versão DOM:Nível DOM1

Mais Exemplos

Se o elemento DIV tiver o primeiro nó filho (índice 0), remova esse nó filho:

// Obter o elemento DIV com id="myDiv"
var div = document.getElementById("myDiv");
// Se o elemento DIV tiver qualquer nó filho, remova seu primeiro nó filho
if (div.hasChildNodes()) {
   div.removeChild(div.childNodes[0]);
}
Testar e Verificar‹/›

Referências Relacionadas

Referência do HTML DOM:elementMétodo .childNodes()

Referência do HTML DOM:nodePropriedade .firstChild

Referência do HTML DOM:nodePropriedade .lastChild

Referência do HTML DOM:nodePropriedade .parentNode

Referência do HTML DOM:nodePropriedade .nextSibling

Referência do HTML DOM:nodePropriedade .previousSibling

Objeto Elemento do HTML DOM