jQuery:在没有浏览器特定代码的情况下,获取输入中的文本的光标位置?

参见英文答案 > Get cursor position (in characters) within a text Input field7个答案在jQuery中有什么办法可以在文本输入中获取当前的游标位置,而不会出现丑陋的浏览器特定代码

像:

<input id="myTextInput" type="text" value="some text" >

光标在“某些文本”的“x”之后,我可以得到“8”?

解决方法

如果没有一些特定于浏览器的代码,则不能执行此操作,因为它们实现的文本选择范围稍有不同。但是,有一些插件可以抽象出来。对于你以后, jQuery Caret (jCaret) plugin

You can try out a demo here

为了让你的代码获得这个职位,你可以这样做:

$("#myTextInput").bind("keydown keypress mousemove",function() {
  alert("Current position: " + $(this).caret().start);
});

You can test it here

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...