获取页面中选中的文字

//获取页面中选中的文字
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); 
}



相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-25
  • 2022-02-26
  • 2022-12-23
  • 2022-03-05
  • 2022-12-23
  • 2022-12-23
  • 2021-10-03
相关资源
相似解决方案