Ans*_*gar 29 jquery input cursor-position
有没有什么方法可以在jQuery中获取文本输入中的当前光标位置,而无需使用丑陋的浏览器特定代码?
像:
<input id="myTextInput" type="text" value="some text" >
Run Code Online (Sandbox Code Playgroud)
光标在"某些文本"的"x"之后,我可以得到"8"?
Nic*_*ver 35
没有你不能做到这一点一些浏览器特定的代码,因为它们实现文本选择略有不同范围.但是,有一些插件可以将其抽象出来.对于你正在追求的东西,有jQuery Caret(jCaret)插件.
要获得该职位的代码,您可以执行以下操作:
$("#myTextInput").bind("keydown keypress mousemove", function() {
alert("Current position: " + $(this).caret().start);
});
Run Code Online (Sandbox Code Playgroud)
Dan*_*ill 15
关于Jquery Caret插件的警告.
它将与Masked Input插件冲突(反之亦然).幸运的是,Masked Input插件包含一个自己的caret()函数,你可以使用非常类似于Caret插件的基本需求 - $(element).caret().begin或.end
Raj*_*aul 14
使用语法,text_element.selectionStart我们可以根据所选文本的第一个字符的索引获取文本选择的起始位置,text_element.value如果我们想要获得选择中我们必须使用的最后一个字符的相同内容text_element.selectionEnd.
使用方法如下:
<input type=text id=t1 value=abcd>
<button onclick="alert(document.getElementById('t1').selectionStart)">check position</button>
Run Code Online (Sandbox Code Playgroud)
我给你fiddle_demo
| 归档时间: |
|
| 查看次数: |
121091 次 |
| 最近记录: |