此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
Element:firstElementChild 属性
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
Element.firstElementChild 只读属性返回元素的第一个子元素,如果没有子元素,则为 null。
Element.firstElementChild 仅包含元素节点。要获取所有子节点(包括文本和注释节点等非元素节点),请使用 Node.firstChild。
值
一个 Element 对象,或 null。
示例
html
<ul id="list">
<li>First (1)</li>
<li>Second (2)</li>
<li>Third (3)</li>
</ul>
<script>
const list = document.getElementById("list");
console.log(list.firstElementChild.textContent);
// 输出"First (1)"
</script>
规范
| Specification |
|---|
| DOM> # ref-for-dom-parentnode-firstelementchild1> |
浏览器兼容性
Loading...