简体   繁体   中英

Get the Highlight text in a specific div

I've not found the answer of my question, so I come to you.

I need to retrieve the selected text in a particularly div when pushing on the F8 key on the keyboard.

Problem, I can get the selected text in the entire document (with window.getSelection()), but is there a way to retrieve the text if and only if the selected text is the div?

Thank you for your answer

May be this may help you:

how to get selection inside a div using jquery/javascript

Using http://code.google.com/p/rangy :

function getSelectedTextWithin(el) {
    var selectedText = "";
    var sel = rangy.getSelection(), rangeCount = sel.rangeCount;
    var range = rangy.createRange();
    range.selectNodeContents(el);
    for (var i = 0; i < rangeCount; ++i) {
        selectedText += sel.getRangeAt(i).intersection(range);
    }
    range.detach();
    return selectedText;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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