简体   繁体   English

使用javascript选择div文本的选择位置

[英]selection position of selected text of div using javascript

I need to get the position of the selected text within a content non-editable div (not a textarea, not a rtf editor, just a simple div) 我需要在内容不可编辑的div中获取所选文本的位置 (不是textarea,不是rtf编辑器,只是一个简单的div)

I want to do this in order to enable users to select pieces of an article and "highlight it", by wrapping it in a span with a different background and, of course, an article is build with divs and/or ps etc, not textareas or rtfs 我想这样做是为了让用户能够选择一篇文章并“突出显示”,将其包装在具有不同背景的跨度中,当然,文章是使用div和/或ps等构建的,而不是textareas或rtfs

Any ideas? 有任何想法吗?

Ps You can also use jQuery :D 你还可以使用jQuery:D

Pss I need the position of the selection, not the selection itself. Pss我需要选择的位置 ,而不是选择本身。 Aka: it start from index I to index J. I need this because the normal method of finding the text in the parent does not always return a unique result, which would suck :) Aka:它从索引I开始到索引J.我需要这个,因为在父级中查找文本的常规方法并不总是返回唯一的结果,这会很糟糕:)

If you just want to change the background of the selected text, the easiest way to do this is by using document.execCommand() . 如果您只想更改所选文本的背景,最简单的方法是使用document.execCommand() See my answer here: Change CSS of selected text using Javascript 请在此处查看我的答案: 使用Javascript更改所选文本的CSS

//Wrap selected text in span tags with the class 'hl'
//Take some action after (in this case, a simple alert)
$("p").live("mouseup",
    function() {
        selection = getSelectedText(); 
        if(selection.length >= 3) {
            $(this).html($(this).html().replace(selection, $('<\/span>').attr({'class':'hl'}).html(selection).parent().html()) );
            alert(selection);
        }       
    }
); 

//Grab selected text
function getSelectedText(){ 
    if(window.getSelection){ 
        return window.getSelection().toString(); 
    } 
    else if(document.getSelection){ 
        return document.getSelection(); 
    } 
    else if(document.selection){ 

        return document.selection.createRange().text; 
    } 
} 

Code comes from here: http://esbueno.noahstokes.com/post/92274686/highlight-selected-text-with-jquery 代码来自这里: http//esbueno.noahstokes.com/post/92274686/highlight-selected-text-with-jquery

Well, even though you found a solution to the problem stated in your 2nd paragraph, i don't think the answer to your main question has been given. 好吧,即使您找到了第2段中所述问题的解决方案,我也不认为您的主要问题的答案已经给出。 :) :)

The object Selection has a property named anchorOffset , giving exactly what you asked for (the position of the selected text within an element). 对象Selection有一个名为anchorOffset的属性,它准确地给出了你要求的内容(元素中所选文本的位置)。 The above link will tell you about which browsers support it, i'm afraid IE <9 might not. 上面的链接将告诉你哪些浏览器支持它,我担心IE <9可能不会。

function show_selected()
{
    var sel = selection();
    console.log(sel.anchorOffset + ':' + sel);
}

Now if you bind show_selected to, say, mouseup , you will see the offset and the selected text printed on the js console. 现在,如果将show_selected绑定到例如mouseup ,您将看到偏移量和js控制台上打印的选定文本。

The fonction selection may be the following, supposed to be cross-browser: 功能selection可能如下,应该是跨浏览器:

function selection()
{
    var sel;
    if(window.getSelection){
      sel = window.getSelection()
    }
    else if(document.getSelection){
      sel = document.getSelection()
    }
    else if(document.selection){
      sel = document.selection.createRange()
    }
    return sel
}

You can check if text is selected by running : 您可以通过运行来检查是否选择了文本:

window.getSelection and document.getSelection() and  document.selection 

(because browsers can check this i different ways) and then search for div containing this text . (因为浏览器可以通过不同的方式检查),然后搜索包含此文本的div。

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

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