[英]Insert text at caret position IE 8
I want to be able to enter some text at the caret position. 我希望能够在插入符号位置输入一些文本。 Currently I can insert text at the end or start of the textbox OR replace the whole textbox content with the new text.
目前,我可以在文本框的末尾或开头插入文本,也可以将整个文本框内容替换为新文本。 How can I get the new text to insert into the textbox at the caret position?
我怎样才能将新文本插入到插入符号位置的文本框中?
What I want: ie textbox contatins "12,3" with caret being where the coma is. 我想要的是:即文本框包含“ 12,3”,而插入符号位于昏迷所在的位置。 After function call it ends up like so: "12new text3"
在函数调用之后,它最终像这样结束:“ 12new text3”
Current code: 当前代码:
var sel = document.getElementById("txtbox");
var text = "new text";
//ie 8
if (document.selection) {
var range = document.selection.createRange();
sel.focus();
range = sel.createTextRange();
range.collapse(false);
range.text = text;
range.select();
}
I've not IE8 to test, but try this: 我没有要测试的IE8,但是请尝试以下操作:
if (document.selection) {
var range = document.selection.createRange();
range.text = text;
range.select();
}
In your code you've defined range
twice, and then collapsed the range
(containing all the text in a textbox) to the end of the text. 在代码中,您两次定义了
range
,然后将range
(包含文本框中的所有文本)折叠到文本的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.