获取网页中选中的文字
生活随笔
收集整理的這篇文章主要介紹了
获取网页中选中的文字
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
獲取頁面中選中的文字
//獲取頁面中選中的文字 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); }轉(zhuǎn)載于:https://www.cnblogs.com/flyjs/archive/2012/02/26/2368583.html
總結(jié)
以上是生活随笔為你收集整理的获取网页中选中的文字的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js 兼容event.target
- 下一篇: 永远的beyond!(4 days le