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 GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
parentElement |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- See implementation notes.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
在一些浏览器上,parentElement
属性只对自身为 Element
的节点定义,也就是说,对文本节点不定义。
规范
Specification |
---|
DOM # ref-for-dom-node-parentelement① |