获取网页中选中的文字

获取页面中选中的文字

//获取页面中选中的文字
function getSelectedText(){
     if(window.getSelection){  //FF
          return window.getSelection().toString();
     }else{ //IE
          return document.selection.createRange().text;
     }
}


设置或获取输入框的选中文字

//设置文字选中
function setSelectText(editor, text) {
    if (!editor) return;
        editor.focus();
    if (editor.document && editor.document.selection)
        editor.document.selection.createRange().text = text;
    else if ("selectionStart" in editor) {
    var str = editor.value, start = editor.selectionStart;
    editor.value = str.substr(0, start) + text + str.substring(editor.selectionEnd, str.length);
    editor.selectionStart = start + text.length;
    editor.selectionEnd = start + text.length;
    }
}
//获取选中的文字
function getSelectText(editor) {
    if (!editor) return; editor.focus();
    if (editor.document && editor.document.selection)
        return editor.document.selection.createRange().text; 
    else if ("selectionStart" in editor)
        return editor.value.substring(editor.selectionStart, editor.selectionEnd); 
}



posted @ 2012-02-26 13:54  mfylee  阅读(1756)  评论(0编辑  收藏  举报