獲取頁面中選中的文字
//獲取頁面中選中的文字 function getSelectedText(){if(window.getSelection){ //FFreturn window.getSelection().toString();}else{ //IEreturn 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); }