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'
}
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. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
节点
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument