1

我希望能够获取在 Web 浏览器中突出显示的文本字符串末尾的 x,y 坐标。我尝试在网上搜索类似的东西,发现 Quora 几乎完全符合我的要求。他们的代码被混淆/压缩了,所以我无法弄清楚他们做了什么。

要查看任何示例,请访问 Quora 上的问题(例如:http ://www.quora.com/Job-Interviews/Whats-the-craziest-thing-you-ever-said-at-an-interview-and- still-got-the-job)并在答案中突出显示一些文本。会弹出一个小框,上面写着“嵌入报价”。获取嵌入报价的位置是我想要做的。

我做了一个小演示来抓取鼠标位置——但如果可能的话,我希望它更准确。这里有一个代码的jsfiddle:http: //jsfiddle.net/BinaryMoon/zy8hY/

$(function(){
    $(document.body).bind('mouseup', function(e){        
        var selection;
        if (window.getSelection) {
            selection = window.getSelection();
        } else if (document.selection) {
            selection = document.selection.createRange();
        }

        if ( selection.toString().length > 3 ) {
            var tip = $('<div class="pointer" />');
            var p = 
            tip.css({
                'position':'absolute',
                'left': e.pageX,
                'top': e.pageY
            });
            tip.appendTo('.post'); 
        }
    });

    $(document.body).bind('mousedown', function(e){
        $('.pointer').remove();
    });
});
4

0 回答 0