element.hasChildNodes
La méthode Node.hasChildNodes() renvoie un Boolean indiquant si le noeud actuel possède des nœuds enfants ou non.
Syntaxe
js
bool = node.hasChildNodes();
Valeur de retour
Un Boolean qui est true si le nœud a des nœuds enfants, et false dans le cas contraire.
Exemple
js
let foo = document.getElementById('foo');
if (foo.hasChildNodes()) {
  // Faire quelque chose avec 'foo.childNodes'
}
Prothèse d'émulation
js
(function(prototype) {
  prototype.hasChildNodes = prototype.hasChildNodes || function() {
    return !!this.firstChild;
  }
})(Node.prototype);
Il y a différentes façons de déterminer si le noeud a un noeud enfant :
- node.hasChildNodes()
- node.firstChild != null(ou simplement- node.firstChild)
- node.childNodes && node.childNodes.length(ou- node.childNodes.length > 0)
Spécifications
| Specification | 
|---|
| DOM Standard # ref-for-dom-node-haschildnodes① | 
Compatibilité des navigateurs
BCD tables only load in the browser