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.startContainer
和 Range.endContainer
相同的节点是目标节点的 共有祖先节点。
语法
rangeAncestor = range.commonAncestorContainer;
示例
js
var range = document.createRange();
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
rangeAncestor = range.commonAncestorContainer;
规范
Specification |
---|
DOM Standard # ref-for-dom-range-commonancestorcontainer② |
浏览器兼容性
BCD tables only load in the browser