如何将光标移动到 JavaScript 中文本区域的最后一个位置?
我在表单上有一个文本区域和一个按钮。文本区域中可能已经有一些文本。我希望单击按钮时光标移动到文本区域中的最后一个位置。
这可能吗?
I have a textarea and a button on a form. The textarea may already have some text in it. I would like the cursor to move to the last position in the text area when the button is clicked.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
xgMz 的答案最适合我。您无需担心浏览器:
这是我编写的一个快速 jQuery 扩展,下次可以为我执行此操作:
像这样使用:
xgMz's answer was best for me. You don't need to worry about the browser:
And here's a quick jQuery extension I wrote to do this for me next time:
Use like this:
“最后一个位置”是指文本的结尾吗?
更改表单字段的“.value”会将光标移动到除 IE 之外的所有浏览器中的结尾。使用 IE,您必须亲自动手并使用非标准接口故意操作选择:
或者您的意思是把光标放回到上次文本区域聚焦时的位置? >
在除 IE 之外的每个浏览器中,只需调用 'element.focus()' 就可以实现这一点;浏览器会记住每个输入的最后一个光标/选择位置并将其放回到焦点上。
在 IE 中重现这将是相当棘手的。您必须始终轮询以检查光标/选择的位置,并记住该位置(如果它位于输入元素中),然后在按下按钮时获取给定元素的位置并恢复它。这涉及到对“document.selection.createRange()”的一些非常繁琐的操作。
我不知道 jQuery 中有什么可以帮助你做到这一点,但也许某个地方有一个插件?
By “the last position”, do you mean the end of the text?
Changing the ‘.value’ of a form field will move the cursor to the end in every browser except IE. With IE you have to get your hands dirty and deliberately manipulate the selection, using non-standard interfaces:
Or do you mean put the cursor back to the place it was previously, the last time the textarea was focused?
In every browser except IE, this will already happen just by calling ‘element.focus()’; the browser remembers the last cursor/selection position per input and puts it back on focus.
This would be quite tricky to reproduce in IE. You would have to poll all the time to check where the cursor/selection was, and remember that position if it was in an input element, then fetch the position for a given element when the button was pressed and restore it. This involves some really quite tedious manipulation of ‘document.selection.createRange()’.
I'm not aware of anything in jQuery that would help you do this, but there might be a plugin somewhere perhaps?
您也可以这样做:
此代码适用于
input
和textarea
。使用最新浏览器进行测试:Firefox 23、IE 11 和 Chrome 28。jsFiddle
可在此处获取: http://jsfiddle.net/cG9gN/1/来源:https://stackoverflow.com/a/12654402/ 114029
You can also do this:
This code works for both
input
andtextarea
. Tested with latest browsers: Firefox 23, IE 11 and Chrome 28.jsFiddle
available here: http://jsfiddle.net/cG9gN/1/Source: https://stackoverflow.com/a/12654402/114029
您可以使用这个 https://github.com/DrPheltRight/jquery-caret
You could use this https://github.com/DrPheltRight/jquery-caret
这是我在 onclick 或其他事件中用于文本区域的方法,并且似乎适用于 FireFox 和 IE 7 & 浏览器。 8. 它将光标定位在文本的末尾而不是前面。
This is what I use for textareas inside my onclick or other events and seems to work fine for FireFox and IE 7 & 8. It positions the cursor at the end of the text instead of the front.
设置鼠标焦点并将光标移动到输入末尾。它适用于每个浏览器,这里只是简单的逻辑。
To set mouse focus and move cursor to end of input. Its work in every browser, Its just simple logic here.
我稍微修改了 @mkaj 扩展以支持默认值
I modified @mkaj extension a little to support default value