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 July 2015.

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 Standard
# ref-for-dom-parentnode-firstelementchild①

浏览器兼容性

BCD tables only load in the browser

参见