ParentNode.firstElementChild
read-only property returns the object's first child
元素
,或
null
if there are no child elements.
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
。在此情况下,
firstElementChild
moved to
ParentNode
. This is a fairly technical change that shouldn't affect compatibility.
var element = node.firstElementChild;
<ul id="foo">
<li>First (1)</li>
<li>Second (2)</li>
<li>Third (3)</li>
</ul>
<script>
var foo = document.getElementById('foo');
// yields: First (1)
console.log(foo.firstElementChild.textContent);
</script>
// Overwrites native 'firstElementChild' prototype.
// Adds Document & DocumentFragment support for IE9 & Safari.
;(function(constructor) {
if (constructor &&
constructor.prototype &&
constructor.prototype.firstElementChild == null) {
Object.defineProperty(constructor.prototype, 'firstElementChild', {
get: function() {
var node, nodes = this.childNodes, i = 0;
while (node = nodes[i++]) {
if (node.nodeType === 1) {
return node;
}
}
return null;
}
});
}
})(window.Node || window.Element);
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'ParentNode.firstElementChild' 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.firstElementChild' in that specification. |
过时 |
Added its initial definition to the
ElementTraversal
pure interface and use it on
元素
.
|
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
firstElementChild
|
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 |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
NonDocumentTypeChildNode.nextElementSibling
ParentNode.lastElementChild
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