使用JavaScript将光标放在文本输入元素中的文本末尾
问题描述:
将焦点设置为元素后,将光标放在输入文本元素中的文本末尾的最佳方法(我假设最简单的方法)是什么?
What is the best way (and I presume simplest way) to place the cursor at the end of the text in a input text element via JavaScript - after focus has been set to the element?
答
我在IE中遇到了同样的问题(在通过RJS / prototype设置焦点之后)。
当字段已有值时,Firefox已将光标留在最后。 IE正在强制光标到文本的开头。
I faced this same issue (after setting focus through RJS/prototype) in IE. Firefox was already leaving the cursor at the end when there is already a value for the field. IE was forcing the cursor to the beginning of the text.
我到达的解决方案如下:
The solution I arrived at is as follows:
<input id="search" type="text" value="mycurrtext" size="30"
onfocus="this.value = this.value;" name="search"/>
这适用于IE7和FF3
This works in both IE7 and FF3