element.hasChildNodes
La méthode Node.hasChildNodes()
renvoie un Boolean
indiquant si le noeud
actuel possède des nœuds enfants ou non.
Syntaxe
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
let foo = document.getElementById('foo');
if (foo.hasChildNodes()) {
// Faire quelque chose avec 'foo.childNodes'
}
Prothèse d'émulation
(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 simplementnode.firstChild
)node.childNodes && node.childNodes.length
(ounode.childNodes.length > 0
)
Spécification
Spécification | Statut | Commentaire |
---|---|---|
DOM La définition de 'Node: hasChildNodes' dans cette spécification. |
Standard évolutif |
Compatibilité des navigateurs
BCD tables only load in the browser