Node.hasChildNodes()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
El método Node.hasChildNodes()
devuelve un valor Boolean indicando si el Node
(nodo) actual tiene nodos hijos o no.
Sintaxis
resultado = node.hasChildNodes();
resultado
-
almacena el valor devuelto
true
ofalse
.
Ejemplos
El siguiente ejemplo elimina el primer nodo dentro del elemento con id "foo"
si foo tiene nodos hijos.
js
var foo = document.getElementById("foo");
if (foo.hasChildNodes()) {
// do something with 'foo.childNodes'
}
Polyfill
js
(function (prototype) {
prototype.hasChildNodes =
prototype.hasChildNodes ||
function () {
return !!this.firstChild;
};
})(Node.prototype);
Resumen
Hay varias maneras de determinar si el nodo tiene nodos hijos.
- node.hasChildNodes()
- node.firstChild != null (o sólo node.firstChild)
- node.childNodes && node.childNodes.length (o node.childNodes.length > 0)
Especificaciones
Specification |
---|
DOM # ref-for-dom-node-haschildnodes① |
Compatibilidad con navegadores
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
hasChildNodes |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.