Fenster.getSelection() von textarea funktioniert nicht in firefox?

Ich versuche, die Auswahl von text auf HTML-Seite.

Ich folgenden code verwenden, und window.getSelection() auf textarea-Nähte nicht in firefox,
aber funktioniert in Google Chrome.

  • Ich bin mit firefox 24 und chrome 27.

Hier ist ein Beispiel:
http://jsfiddle.net/AVLCY/

HTML:

<div>Text in div</div>
<textarea>Hello textarea</textarea>
<div id='debug'></div>

JS:

$(document).on('mouseup','body',function(){
   $("#debug").html("You select '" + getSelectionText() + "'");
});

function getSelectionText() {
    if (window.getSelection) {
        try {
            //return "" in firefox
            return window.getSelection().toString();
        } catch (e) {
            console.log('Cant get selection text')
        }
    } 
    //For IE
    if (document.selection && document.selection.type != "Control") {
        return document.selection.createRange().text;
    }
}
InformationsquelleAutor user3047765 | 2013-12-06
Schreibe einen Kommentar