ParentNode
property
children
is a read-only property that returns a live
HTMLCollection
which contains all of the child
元素
of the node upon which it was called.
let children = node.children;
HTMLCollection
which is a live, ordered collection of the DOM elements which are children of
node
. You can access the individual child nodes in the collection by using either the
item()
method on the collection, or by using JavaScript array-style notation.
If the node has no element children, then
children
is an empty list with a
length
of
0
.
const foo = document.getElementById('foo');
for (let i = 0; i < foo.children.length; i++) {
console.log(foo.children[i].tagName);
}
// Overwrites native 'children' prototype.
// Adds Document & DocumentFragment support for IE9 & Safari.
// Returns array instead of HTMLCollection.
;(function(constructor) {
if (constructor &&
constructor.prototype &&
constructor.prototype.children == null) {
Object.defineProperty(constructor.prototype, 'children', {
get: function() {
let i = 0, node, nodes = this.childNodes, children = [];
while (node = nodes[i++]) {
if (node.nodeType === 1) {
children.push(node);
}
}
return children;
}
});
}
})(window.Node || window.Element);
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'ParentNode.children' in that specification. |
实时标准 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
children
|
Chrome 1 | Edge 12 | Firefox 3.5 |
IE
9
|
Opera 10 | Safari 4 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 9 | Samsung Internet Android 1.0 |
Support on
Document
and
DocumentFragment
|
Chrome 29 | Edge 16 | Firefox 25 | IE No | Opera 16 | Safari 9 | WebView Android Yes | Chrome Android Yes | Firefox Android ? | Opera Android ? | Safari iOS 9 | Samsung Internet Android Yes |
Support on
SVGElement
|
Chrome Yes | Edge 16 | Firefox Yes | IE No | Opera ? | Safari 9 | WebView Android Yes | Chrome Android Yes | Firefox Android ? | Opera Android ? | Safari iOS 9 | Samsung Internet Android Yes |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
见实现注意事项。
ParentNode
and
ChildNode
接口。
Document
,
元素
,和
DocumentFragment
.
Node.childNodes
ParentNode
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