简体   繁体   English

插入单词后的定位光标

[英]Positioning cursor after inserted word

I want to position a cursor after an inserted word (test), in textarea. 我想在textarea中的插入单词(测试)之后放置光标。
The insertion of the word can be in any position in textarea. 单词的插入可以在文本区域的任何位置。
(Internet Explorer) (IE浏览器)
This is my script: 这是我的脚本:

 document.activeElement.focus();

 document.selection.createRange().text = "test";

 var sel = document.selection.createRange();
 sel.moveStart('character', -document.activeElement.value.length);
 var cursorPos = sel.text.length;

 var range = this.textarea.createTextRange();
 range.collapse(true);
 range.moveEnd('character', cursorPos);
 range.moveStart('character', cursorPos);
 range.select();

This will do it (in Internet Explorer only, you'll need a totally different approach for other browsers): 可以做到这一点(仅在Internet Explorer中,对于其他浏览器,您将需要一种完全不同的方法):

document.activeElement.focus();
var sel = document.selection.createRange();
sel.text = "test";
sel.collapse(false);
sel.select();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM