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 1Edge Full support 12Firefox Full support 9IE Full support 9
Notes
Full support 9
Notes
Notes Only supported on Element.
Opera Full support 7Safari Full support 1.1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 9Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0

Legend

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

规范

参见