简体   繁体   English

获取相对于父元素的点击范围

[英]Get click range relative to parent element

So I have this problem where I need to get the clicked spot offset relative to the parent element.所以我遇到了这个问题,我需要获取相对于父元素的点击点偏移量。 Lets assume I have html rendered like that:让我们假设我有这样呈现的 html:

<div>
   able   to   allocate   many   IPs.
   <span style="background: yellow">
      Proof-of-work   is   essentially   one-CPU-one-vote.
   </span>     The   majority
</div>

I want to get the offset of click relative to the parent div.我想获得相对于父 div 的点击偏移量。 For example if I click the word is in span tags I get the offset of the click relative to the parent Div.例如,如果我点击单词在 span 标签中,我会得到点击相对于父 Div 的偏移量。 How can I implement this.我该如何实现这一点。 I have tried to write code like this:我曾尝试编写这样的代码:

var selection = window.getSelection();
var range = selection.getRangeAt(0);
var offset = range.startOffset;

But this gives me the offset relative to the span tag.但这给了我相对于跨度标签的偏移量。 So how can I get the offset relative to the div tag?那么如何获得相对于 div 标签的偏移量呢?

I am not aware of any built-in functions that can measure the offset of a Range relative to another element.我不知道有任何内置函数可以测量 Range 相对于另一个元素的偏移量。 You can use the Range's startContainer property and then crawl up and across the DOM hierarchy, measuring the content as you go along.您可以使用 Range 的startContainer属性,然后向上爬行并跨越 DOM 层次结构,在进行过程中测量内容。

I created a demo below.我在下面创建了一个演示。 Note, however, that this may or may not produce exactly the result you are looking for.但是请注意,这可能会也可能不会产生您正在寻找的结果。 For example, this will measure the length of comments and style nodes, and it will count all the space characters, whether or not the browser ultimately renders them.例如,这将测量注释和样式节点的长度,并将计算所有空格字符,无论浏览器最终是否呈现它们。 You'll probably need to make adjustments to suit your own purposes by adding checks for the nodeType before measuring, pre-collapsing space characters (perhaps using regex replace), etc, but I hope this is a good starting place.您可能需要通过在测量之前添加对nodeType 的检查、预折叠空格字符(可能使用正则表达式替换)等来进行调整以适应您自己的目的,但我希望这是一个好的起点。

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM