admin管理员组文章数量:1391937
in firefox and chrome window.getSelection is used to get the current selection in document,but is the current selection is in a textarea,the window.getSelection didn't return the selection,but the textarea itself. So,how to the right selection in firefox and chrome?
in firefox and chrome window.getSelection is used to get the current selection in document,but is the current selection is in a textarea,the window.getSelection didn't return the selection,but the textarea itself. So,how to the right selection in firefox and chrome?
Share Improve this question asked May 15, 2012 at 8:10 Guan YuxinGuan Yuxin 4306 silver badges12 bronze badges2 Answers
Reset to default 5Do you need to get the selected text in a textarea? You may be asking for selectionStart and selectionEnd (does not exist in Internet Explorer, works with Firefox and Chrome)
Select some text below and then click the button:<br/>
<textarea id="myTextarea" rows="5" cols="30">
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
</textarea>
<button onclick="alert(getTextSelection())">alert text selection</button>
<script type="text/javascript">
function getTextSelection(){
var field = document.getElementById("myTextarea");
var startPos = field.selectionStart;
var endPos = field.selectionEnd;
var field_value = field.value;
var selectedText = field_value.substring(startPos,endPos);
return selectedText;
}
</script>
If there are multiple textareas and you wish to get the output on select:
Select some text in either textarea:<br/>
<textarea rows="5" cols="30" onselect="alert(getTextSelection(this))">
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
</textarea>
<textarea rows="5" cols="30" onselect="alert(getTextSelection(this))">
fate it seems
not without a sense of irony
</textarea>
<script type="text/javascript">
function getTextSelection(field){
var startPos = field.selectionStart;
var endPos = field.selectionEnd;
var selectedText = field.value.substring(startPos,endPos);
return selectedText;
}
</script>
Or you can still do it with a button but by using a global variable:
Select some text in either textarea and click the button:<br/>
<textarea rows="5" cols="30" onselect="window.selectedTextarea=this">
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
</textarea>
<textarea rows="5" cols="30" onselect="window.selectedTextarea=this">
fate it seems
not without a sense of irony
</textarea>
<button onclick="alert(getTextSelection())">alert text selection</button>
<script type="text/javascript">
// warning: global variable: dirty!!!
var selectedTextarea
function getTextSelection(){
var field = window.selectedTextarea;
var startPos = field.selectionStart;
var endPos = field.selectionEnd;
var selectedText = field.value.substring(startPos,endPos);
return selectedText;
}
</script>
Textareas and text inputs have a differenct selection API. They have selectionStart
and selectionEnd
properties that are character offsets within the value
property of the textarea / input. These properties have been standardized in HTML5 and are implemented by the current versions of all major browsers, although IE < 9 has a different API again.
本文标签: javascriptwindowgetSelection get the right selection in textareaStack Overflow
版权声明:本文标题:javascript - window.getSelection get the right selection in textarea - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744743041a2622723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论