node.hasChildNodes()
node.firstClild !== null
node.childNodes && node.childNodes.length > 0
判断一个节点有没有子节点的方法
hasClildNodes方法结合firstChild属性和nextSibling属性,可以遍历当前节点的所有后代节点。
function DOMComb(parent, callback) {
if (parent.hasChildNodes()) {
for (var node = parent.firstChild; node; node.nextSibling) {
DOMComb(node, callback);
}
}
callback(parent);
}
//用法
DOMComb(document.body, console.log)