如何将 onclick 鼠标位置与确切的 DOM 元素内容相关联?
使用 jquery 我尝试创建一个插件,它可以执行以下操作:给定一个任意网页,该网页主要是基于文本的,如维基百科等。基本上我说的是浏览器呈现的纯 HTML。现在,如果用户单击网站上的某个位置,例如在文本段落的有趣句子(或单词)的中间,该插件会检测到此单击 - 此时我认为我正在谈论一个简单的 onclick事件。
我现在想做的是:插件不仅知道用户点击了,而且还知道 DOM 树中的确切位置。这意味着,例如,我想知道单击与包含字符串的 span 元素相关,并且在该字符串中用户在字符 4 和 5 之间单击。
我找到了一个显示非常好的图像 我尝试做什么。
有了这样的插件,人们可以做很多事情,例如:突出显示一些文本,就像在学习书籍时所做的那样,或者在不同网站上的文本段落之间创建突出显示的相关性,... 但目前我只是在考虑如何收集浏览器中可视化所需的数据,
谢谢您的任何提示
Using jquery I try to create a plugin which can do the following: given an arbitrary webpage which is mostly text-based like wikipedia or such. Basically I'm talking about plain HTML rendered by the browser. Now, if the user clicks somewhere on the website, like in the middle of an interesting sentence (or word) of a text passage, the plugin detects this click - at this point I think I'm talking about a simple onclick
event.
What I now want to do is: the plugin not only knows that the user clicked but also exactly where within the DOM tree. this means for example that i want to know that the click is correlated to a span-element which contains a string AND within that string the user clicked between character 4 and 5.
I found an image which shows pretty good what I try to do.
With such a plugin there are plenty of things one can do like: highlight some text like you do when learning from a book or create highlighted correlations between text passages on different websites, ...
but currently i'm just thinking about how I could gather the data needed for visualization within the browser
thx for any hint on this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以从以下内容开始侦听文档中任何位置的点击:
问题是,任何其他具有
onclick
事件的内容都需要阻止该事件传播到您的“主”点击处理程序。这里有一个 JSFiddle 可以帮助您入门。
You can listen for clicks anywhere in a document by starting with something like:
The problem there is anything else that had an
onclick
event needs to prevent the event from propagating to your "master" click handler.Here's a JSFiddle to get you started.