English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
node.hasChildNodes()
var div = document.getElementById("myDiv"); div.hasChildNodes();Testar e Verificar‹/›
All browsers fully support the hasChildNodes() method:
Method | |||||
hasChildNodes() | Is | Is | Is | Is | Is |
Return value: | Boolean value, returns true if the node has child nodes, otherwise returns false |
---|---|
Versão DOM: | Nível DOM1 |
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ê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