Range.commonAncestorContainer
read-only property returns the deepest — or furthest down the document tree —
节点
that contains both
boundary points
的
范围
. This means that if
Range.startContainer
and
Range.endContainer
both refer to the same node, this node is the
common ancestor container
.
Since a
范围
need not be continuous, and may also partially select nodes, this is a convenient way to find a
节点
which encloses a
范围
.
This property is read-only. To change the ancestor container of a
节点
, consider using the various methods available to set the start and end positions of the
范围
,譬如
Range.setStart()
and
Range.setEnd()
.
rangeAncestor = range.commonAncestorContainer;
In this example, we create an event listener to handle
pointerup
events on a list. The listener gets the common ancestors of each piece of selected text, and triggers an animation to highlight them.
<ul>
<li>Strings
<ul>
<li>Cello</li>
<li>Violin
<ul>
<li>First Chair</li>
<li>Second Chair</li>
</ul>
</li>
</ul>
</li>
<li>Woodwinds
<ul>
<li>Clarinet</li>
<li>Oboe</li>
</ul>
</li>
</ul>
.highlight
class created below uses a set of CSS
@keyframes
to animate a fading outline.
.highlight {
animation: highlight linear 1s;
}
@keyframes highlight {
from { outline: 1px solid #f00f; }
to { outline: 1px solid #f000; }
}
body {
padding: 1px;
}
document.addEventListener('pointerup', e => {
const selection = window.getSelection();
if (selection.type === 'Range') {
for (let i = 0; i < selection.rangeCount; i++) {
const range = selection.getRangeAt(i);
playAnimation(range.commonAncestorContainer);
}
}
});
function playAnimation(el) {
if (el.nodeType === Node.TEXT_NODE) {
el = el.parentNode;
}
el.classList.remove('highlight');
setTimeout(() => {
el.classList.add('highlight');
}, 0);
}
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
commonAncestorContainer
|
Chrome Yes | Edge 12 | Firefox 4 | IE 9 | Opera 9 | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 4 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
范围
collapsed
commonAncestorContainer
endContainer
endOffset
startContainer
startOffset
cloneContents()
cloneRange()
collapse()
compareBoundaryPoints()
compareNode()
comparePoint()
createContextualFragment()
deleteContents()
detach()
extractContents()
getBoundingClientRect()
getClientRects()
insertNode()
intersectsNode()
isPointInRange()
selectNode()
selectNodeContents()
setEnd()
setEndAfter()
setEndBefore()
setStart()
setStartAfter()
setStartBefore()
surroundContents()
toString()
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument