[英]getting caret position with spaces in IE
The problem is with spaces. 问题在于空间。 Sometimes if the caret is on a space, it ignores the space and finds the position of the last character (that isn't a space).
有时如果插入符号在空格上,它会忽略空格并找到最后一个字符的位置(这不是空格)。 In this demo if you click just right of the letter c it shows caret position as 3. Then click just left of the letter d (one character along) and it still shows caret position as 3. However if you click left of d first, then it shows position 4 (the correct position).
在本演示中,如果单击字母c的右侧,则将插入符号位置显示为3.然后单击字母d左侧(沿着一个字符),它仍显示插入符号位置为3.但是如果单击左侧第一个,然后它显示位置4(正确的位置)。 If anyone can take a look at my jsFiddle and try this out maybe you can find a solution.
如果有人可以看看我的jsFiddle,试试这个也许你可以找到一个解决方案。 Thank you.
谢谢。
NB this code only works in IE. 注意,此代码仅适用于IE。
$('input').click(function(){
var Sel = document.selection.createRange();
Sel.moveStart('character', -this.value.length);
CaretPos = Sel.text.length;
$('span').text(CaretPos);
}); });
The selectionStart
worked in latest IE, Firefox & Chrome. 该
selectionStart
曾在最新的IE,Firefox和Chrome浏览器。
$('input').click(function(){
this.focus();
/**var Sel = document.selection.createRange();
Sel.moveStart('character', -inelem.value.length);
CaretPos = Sel.text.length;*/
CaretPos = this.selectionStart;
$('span').text(CaretPos);
});
But, if you are concerned about supporting older IE versions, you can conditionally invoke createRange
over selectionStart
. 但是,如果您担心支持旧的IE版本,则可以有条件
createRange
在selectionStart
调用createRange
。 Check this version of fiddle. 检查这个版本的小提琴。 The
doGetCaretPosition
is based on this link doGetCaretPosition
基于此链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.