Node.hasChildNodes() 方法返回 布尔 value indicating whether the given 节点 has child nodes or not.

句法

bool = node.hasChildNodes();
					

返回值

A 布尔 也就是 true if the node has child nodes, and false 否则。

范例

let foo = document.getElementById('foo');
if (foo.hasChildNodes()) {
  // Do something with 'foo.childNodes'
}
					

Polyfill

Here is one possible polyfill:

;(function(prototype) {
  prototype.hasChildNodes = prototype.hasChildNodes || function() {
    return !!this.firstChild;
  }
})(Node.prototype);
					

There are various ways to determine whether the node has a child node:

  • node.hasChildNodes()
  • node.firstChild != null (or just node.firstChild )
  • node.childNodes && node.childNodes.length (或 node.childNodes.length > 0 )

规范

规范 状态 注释
DOM
The definition of 'Node: hasChildNodes' in that specification.
实时标准

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
hasChildNodes Chrome 1 Edge 12 Firefox Yes IE 9 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: