Node.parentElement

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.

返回当前节点的父元素节点,如果该元素没有父节点,或者父节点不是一个 DOM 元素,则返回 null

语法

parentElement = node.parentElement

parentElement 是当前节点的父元素。它永远是一个 DOM 元素 对象,或者 null

例子

js
if (node.parentElement) {
  node.parentElement.style.color = "red";
}

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
parentElement

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

在一些浏览器上,parentElement 属性只对自身为 Element 的节点定义,也就是说,对文本节点不定义。

规范

Specification
DOM
# ref-for-dom-node-parentelement①

参见