获取相对于父元素的点击范围
Get click range relative to parent element
所以我遇到了这个问题,我需要获得相对于父元素的点击点偏移量。
让我们假设我有 html 那样渲染:
<div>
able to allocate many IPs.
<span style="background: yellow">
Proof-of-work is essentially one-CPU-one-vote.
</span> The majority
</div>
我想获取点击相对于父级的偏移量div。例如,如果我点击单词 is in span 标签,我会得到点击相对于父标签的偏移量 Div。我该如何实施。
我试过这样写代码:
var selection = window.getSelection();
var range = selection.getRangeAt(0);
var offset = range.startOffset;
但这给了我相对于 span 标签的偏移量。那么如何获得相对于 div 标签的偏移量呢?
我不知道有任何 built-in 函数可以测量 Range 相对于另一个元素的偏移量。您可以使用 Range 的 startContainer
属性,然后在 DOM 层次结构中向上爬行,同时测量内容。
我在下面创建了一个演示。但是请注意,这可能会或可能不会产生您正在寻找的结果。例如,这将测量注释和样式节点的长度,并将计算所有 space 个字符,无论浏览器是否最终呈现它们。您可能需要根据自己的目的进行调整,方法是在测量前添加 nodeType、pre-collapsing space 字符(可能使用正则表达式替换)等检查,但我希望这是一个很好的起点。
function clicked(){
console.log( getSelectionOffsetRelativeTo( document.getElementById( 'parent' ) ) );
}
function getSelectionOffsetRelativeTo(parentElement, currentNode){
var currentSelection, currentRange,
offset = 0,
prevSibling,
nodeContent;
if (!currentNode){
currentSelection = window.getSelection();
currentRange = currentSelection.getRangeAt(0);
currentNode = currentRange.startContainer;
offset += currentRange.startOffset;
}
if (currentNode === parentElement){
return offset;
}
if (!parentElement.contains(currentNode)){
return -1;
}
while ( prevSibling = (prevSibling || currentNode).previousSibling ){
nodeContent = prevSibling.innerText || prevSibling.nodeValue || "";
offset += nodeContent.length;
}
return offset + getSelectionOffsetRelativeTo( parentElement, currentNode.parentNode );
}
Click inside the div:
<div id="parent" style="border: black 1px solid;" onclick="javascript:clicked();">
Currently in the parent node.
<span style="color: red;">In the first child span.</span>
Back in the parent node.
<span style="color: red;">In the second child span.
<span style="font-style:italic;">In the grandchild span.</span>
</span>
Back in the parent node.
</div>
所以我遇到了这个问题,我需要获得相对于父元素的点击点偏移量。 让我们假设我有 html 那样渲染:
<div>
able to allocate many IPs.
<span style="background: yellow">
Proof-of-work is essentially one-CPU-one-vote.
</span> The majority
</div>
我想获取点击相对于父级的偏移量div。例如,如果我点击单词 is in span 标签,我会得到点击相对于父标签的偏移量 Div。我该如何实施。 我试过这样写代码:
var selection = window.getSelection();
var range = selection.getRangeAt(0);
var offset = range.startOffset;
但这给了我相对于 span 标签的偏移量。那么如何获得相对于 div 标签的偏移量呢?
我不知道有任何 built-in 函数可以测量 Range 相对于另一个元素的偏移量。您可以使用 Range 的 startContainer
属性,然后在 DOM 层次结构中向上爬行,同时测量内容。
我在下面创建了一个演示。但是请注意,这可能会或可能不会产生您正在寻找的结果。例如,这将测量注释和样式节点的长度,并将计算所有 space 个字符,无论浏览器是否最终呈现它们。您可能需要根据自己的目的进行调整,方法是在测量前添加 nodeType、pre-collapsing space 字符(可能使用正则表达式替换)等检查,但我希望这是一个很好的起点。
function clicked(){
console.log( getSelectionOffsetRelativeTo( document.getElementById( 'parent' ) ) );
}
function getSelectionOffsetRelativeTo(parentElement, currentNode){
var currentSelection, currentRange,
offset = 0,
prevSibling,
nodeContent;
if (!currentNode){
currentSelection = window.getSelection();
currentRange = currentSelection.getRangeAt(0);
currentNode = currentRange.startContainer;
offset += currentRange.startOffset;
}
if (currentNode === parentElement){
return offset;
}
if (!parentElement.contains(currentNode)){
return -1;
}
while ( prevSibling = (prevSibling || currentNode).previousSibling ){
nodeContent = prevSibling.innerText || prevSibling.nodeValue || "";
offset += nodeContent.length;
}
return offset + getSelectionOffsetRelativeTo( parentElement, currentNode.parentNode );
}
Click inside the div:
<div id="parent" style="border: black 1px solid;" onclick="javascript:clicked();">
Currently in the parent node.
<span style="color: red;">In the first child span.</span>
Back in the parent node.
<span style="color: red;">In the second child span.
<span style="font-style:italic;">In the grandchild span.</span>
</span>
Back in the parent node.
</div>