ParentNode.childElementCount
read-only property returns an
unsigned long
representing the number of child elements of the given element.
This property was initially defined in the
ElementTraversal
pure interface. As this interface contained two distinct set of properties, one aimed at
节点
that have children, one at those that are children, they have been moved into two separate pure interfaces,
ParentNode
and
ChildNode
。在此情况下,
childElementCount
moved to
ParentNode
. This is a fairly technical change that shouldn't affect compatibility.
var count = node.childElementCount;
count
unsigned long
(simply an integer) type.
node
Document
,
DocumentFragment
,或
元素
.
var foo = document.getElementById('foo');
if (foo.childElementCount > 0) {
// Do something
}
This property is completely unsupported prior to IE9. In IE9 and Safari, it is unsupported in the
Document
and
DocumentFragment
对象。
;(function(constructor) {
if (constructor &&
constructor.prototype &&
constructor.prototype.childElementCount == null) {
Object.defineProperty(constructor.prototype, 'childElementCount', {
get: function() {
var i = 0, count = 0, node, nodes = this.childNodes;
while (node = nodes[i++]) {
if (node.nodeType === 1) count++;
}
return count;
}
});
}
})(window.Node || window.Element);
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'ParentNode.childElementCount' in that specification. |
实时标准 |
Split the
ElementTraversal
interface in
ChildNode
and
ParentNode
. This method is now defined on the latter.
Document
and
DocumentFragment
implemented the new interfaces.
|
|
Element Traversal Specification
The definition of 'ElementTraversal.childElementCount' in that specification. |
过时 |
Added its initial definition to the
ElementTraversal
pure interface and use it on
元素
.
|
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
childElementCount
|
Chrome 1 | Edge 12 | Firefox 3.5 | IE 9 | Opera 10 | Safari 4 | WebView Android Yes | Chrome Android Yes | Firefox Android 4 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
Support on
Document
and
DocumentFragment
|
Chrome 29 | Edge ≤79 | Firefox 25 | IE No | Opera 16 | Safari No | WebView Android Yes | Chrome Android Yes | Firefox Android ? | Opera Android ? | Safari iOS No | Samsung Internet Android Yes |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
ParentNode
and
ChildNode
pure interfaces.
Document
,
元素
,和
DocumentFragment
.
ParentNode
childElementCount
children
firstElementChild
lastElementChild
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