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

时间:2021-10-29 17:23:50

This question already has an answer here:

这个问题已经有了答案:

Is there any way in jQuery to get the current cursor-position in an text input without doing ugly browser specific code?

在jQuery中,是否有办法在文本输入中获取当前指针位置,而不需要编写难看的浏览器特定代码?

Like in:

就像:

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

the cursor being after the "x" of "some text", I can get "8"?

光标在“某文本”的“x”后面,我能得到“8”吗?

4 个解决方案

#1


32  

You can't do this without some browser specific code, since they implement text select ranged slightly differently. However, there are plugins that abstract this away. For exactly what you're after, there's the jQuery Caret (jCaret) plugin.

如果没有特定于浏览器的代码,您就不能这样做,因为它们实现的文本选择范围略有不同。但是,有一些插件会将这一点抽象出来。确切地说,您需要的是jQuery插入符号(jCaret)插件。

You can try out a demo here.

您可以在这里尝试一个演示。

For your code to get the position you could do something like this:

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

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

You can test it here.

你可以在这里测试。

#2


15  

A warning about the Jquery Caret plugin.

关于Jquery插入符号的警告。

It will conflict with the Masked Input plugin (or vice versa). Fortunately the Masked Input plugin includes a caret() function of its own, which you can use very similarly to the Caret plugin for your basic needs - $(element).caret().begin or .end

它将与屏蔽输入插件发生冲突(反之亦然)。幸运的是,屏蔽输入插件包含了一个插入符号()函数,您可以使用它来满足您的基本需求——$(元素).caret()。开始或.end

#3


12  

Using the syntax text_element.selectionStart we can get the starting position of the selection of a text in terms of the index of the first character of the selected text in the text_element.value and in case we want to get the same of the last character in the selection we have to use text_element.selectionEnd.

使用语法text_element。selectionStart我们可以从text_element中所选文本的第一个字符的索引中获得文本选择的起始位置。值,如果我们想在选择中获得相同的最后一个字符,我们必须使用text_element.selectionEnd。

Use it as follows:

使用如下:

<input type=text id=t1 value=abcd>
<button onclick="alert(document.getElementById('t1').selectionStart)">check position</button>

I'm giving you the fiddle_demo

我给你们演示一下fiddle_demo

#4


7  

just came across while browsing, might help you javascript-getting-and-setting-caret-position-in-textarea. You can use it for textbox also.

刚刚在浏览时遇到的,可能会帮助你在文本区域中找到并设置位置。你也可以用它作为文本框。

#1


32  

You can't do this without some browser specific code, since they implement text select ranged slightly differently. However, there are plugins that abstract this away. For exactly what you're after, there's the jQuery Caret (jCaret) plugin.

如果没有特定于浏览器的代码,您就不能这样做,因为它们实现的文本选择范围略有不同。但是,有一些插件会将这一点抽象出来。确切地说,您需要的是jQuery插入符号(jCaret)插件。

You can try out a demo here.

您可以在这里尝试一个演示。

For your code to get the position you could do something like this:

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

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

You can test it here.

你可以在这里测试。

#2


15  

A warning about the Jquery Caret plugin.

关于Jquery插入符号的警告。

It will conflict with the Masked Input plugin (or vice versa). Fortunately the Masked Input plugin includes a caret() function of its own, which you can use very similarly to the Caret plugin for your basic needs - $(element).caret().begin or .end

它将与屏蔽输入插件发生冲突(反之亦然)。幸运的是,屏蔽输入插件包含了一个插入符号()函数,您可以使用它来满足您的基本需求——$(元素).caret()。开始或.end

#3


12  

Using the syntax text_element.selectionStart we can get the starting position of the selection of a text in terms of the index of the first character of the selected text in the text_element.value and in case we want to get the same of the last character in the selection we have to use text_element.selectionEnd.

使用语法text_element。selectionStart我们可以从text_element中所选文本的第一个字符的索引中获得文本选择的起始位置。值,如果我们想在选择中获得相同的最后一个字符,我们必须使用text_element.selectionEnd。

Use it as follows:

使用如下:

<input type=text id=t1 value=abcd>
<button onclick="alert(document.getElementById('t1').selectionStart)">check position</button>

I'm giving you the fiddle_demo

我给你们演示一下fiddle_demo

#4


7  

just came across while browsing, might help you javascript-getting-and-setting-caret-position-in-textarea. You can use it for textbox also.

刚刚在浏览时遇到的,可能会帮助你在文本区域中找到并设置位置。你也可以用它作为文本框。