如何将 onclick 鼠标位置与确切的 DOM 元素内容相关联?

发布于 2024-12-19 05:47:46 字数 525 浏览 0 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城°AllureLove 2024-12-26 05:47:46

您可以从以下内容开始侦听文档中任何位置的点击:

$(function() {
    $(document).click(function(evt) {
        var elementClicked = evt.target;
        var xCoordinate = evt.pageX;
        var yCoordinate = evt.pageY;

        alert('You clicked a ' + elementClicked.tagName + 
            ' at {' + xCoordinate + ',' + yCoordinate + '}');

        // Do work to figure out what the element was and where within it was clicked
        switch (elementClicked.tagName.toLowerCase()) {
            case "input":
            case "span":
            case "button":
            case "label":
            case "select":
            case "textarea":
            case "p":
            ...
        }
    });
});

问题是,任何其他具有 onclick 事件的内容都需要阻止该事件传播到您的“主”点击处理程序。

这里有一个 JSFiddle 可以帮助您入门。

You can listen for clicks anywhere in a document by starting with something like:

$(function() {
    $(document).click(function(evt) {
        var elementClicked = evt.target;
        var xCoordinate = evt.pageX;
        var yCoordinate = evt.pageY;

        alert('You clicked a ' + elementClicked.tagName + 
            ' at {' + xCoordinate + ',' + yCoordinate + '}');

        // Do work to figure out what the element was and where within it was clicked
        switch (elementClicked.tagName.toLowerCase()) {
            case "input":
            case "span":
            case "button":
            case "label":
            case "select":
            case "textarea":
            case "p":
            ...
        }
    });
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文