Range.commonAncestorContainer

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

Range.commonAncestorContainer 只读属性,返回目标节点的共有祖先节点。因而需要注意:selectNode 方法中的该值为目标节点的父节点,selectNodeContents 方法中的该值为其本身。

在某些跨节点的选取操作时,取得最大"公约数"的节点为commonAncestorContainer。Range.startContainerRange.endContainer 相同的节点是目标节点的 共有祖先节点。

更改 Node, 请使用 setStart setEnd 及这两种方法的延伸方法 Range.

语法

rangeAncestor = range.commonAncestorContainer;

示例

js
var range = document.createRange();

range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
rangeAncestor = range.commonAncestorContainer;

规范

Specification
DOM
# ref-for-dom-range-commonancestorcontainer②

浏览器兼容性

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
commonAncestorContainer

Legend

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

Full support
Full support

更多