DOM - 查看当前节点下有无子元素节点

  1. for 循环版
Element.prototype.hasChildren = function () {
  const childNodes = this.childNodes,
        len = childNodes.length;

  for (let i = 0; i < len; i++) {
    const item = childNodes[i];

    if (item.nodeType === 1) {
      return true;
    }
  }

  return false;
}
  1. while 循环版
Element.prototype.hasChildren = function () {
  const childNodes = this.childNodes,
        len = childNodes.length;

  while (len) {
 	item = childNodes[len - 1]; // 倒着遍历
 	
    if (item.nodeType === 1) {
      return true;
    }
    
    len--;
  }

  return false;
}