此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Node:parentNode 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

Node 接口的 parentNode 只读属性返回该节点在 DOM 树中的父节点。

DocumentDocumentFragment 节点永远不会有父节点,因此 parentNode 始终会返回 null。如果节点刚被创建、尚未被插入到树中时,也会返回 null。相比之下,Node.parentElement 只会返回 Element 类型的父节点。

一个表示当前节点的父节点的 Node。元素的父节点可能是 Element 节点、Document 节点或 DocumentFragment 节点。

示例

使用 parentNode

此示例会将一个节点从树中移除(如果该节点尚在树中)。

js
if (node.parentNode) {
  node.parentNode.removeChild(node);
}

规范

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

浏览器兼容性

参见