Node.parentElement

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

语法

parentElement = node.parentElement

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

例子

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

浏览器兼容性

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

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
parentElementChrome Full support YesEdge Full support 12Firefox Full support 9IE Full support Yes
Notes
Full support Yes
Notes
Notes Only supported on Element.
Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 9Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
See implementation notes.
See implementation notes.

规范

参见