通过 JavaScript 将光标置于输入文本元素中的文本末尾的最佳方法(我认为是最简单的方法)是什么 - 在焦点设置到元素之后?
使用 JavaScript 将光标置于文本输入元素中的文本末尾
有一种简单的方法可以让它在大多数浏览器中工作。
this.selectionStart = this.selectionEnd = this.value.length;
但是,由于一些浏览器的 * 怪癖,更具包容性的答案看起来更像这样
setTimeout(function(){ that.selectionStart = that.selectionEnd = 10000; }, 0);
使用 jQuery (设置侦听器,否则没有必要)
使用 Vanilla JS  (addEvent从这个答案中借用功能)
怪癖
Chrome 有一个奇怪的怪癖,即在光标移入该字段之前触发焦点事件;这搞砸了我的简单解决方案。解决此问题的两个选项:
- 您可以添加 0 毫秒的超时(将操作推迟到堆栈清除)
 - 您可以将事件从 更改
focus为mouseup。除非您仍然保持关注焦点,否则这对用户来说会很烦人。我并不是很喜欢这两种选择中的任何一种。 
此外,@vladkras 指出,某些旧版本的 Opera 会在有空格时错误地计算长度。为此,您可以使用一个比字符串大的数字。
我在 IE 中遇到了同样的问题(在通过 RJS/原型设置焦点之后)。当该字段已经有一个值时,Firefox 已经在最后离开了光标。IE 强制光标到文本的开头。
我得到的解决方案如下:
<input id="search" type="text" value="mycurrtext" size="30" 
       onfocus="this.value = this.value;" name="search"/>
这适用于 IE7 和 FF3
试试这个,它对我有用:
//input is the input element
input.focus(); //sets focus to element
var val = this.input.value; //store the value of the element
this.input.value = ''; //clear the value of the element
this.input.value = val; //set that value back.  
要使光标移动到末尾,输入必须先有焦点,然后当值改变时它会移动到末尾。如果您将 .value 设置为相同,则它不会在 chrome 中发生变化。
稍微研究了一下之后,我发现最好的方法是setSelectionRange在浏览器支持的情况下使用该功能;如果没有,请恢复使用 Mike Barrow 的答案中的方法(即用自身替换该值)。
我也设置scrollTop为高值的情况下,我们正处在一个垂直滚动textarea。(使用任意高值似乎比$(this).height()在 Firefox 和 Chrome 中更可靠。)
我已经把它作为一个 jQuery 插件。(如果你不使用 jQuery,我相信你仍然可以很容易地获得要点。)
我已经在 IE6、IE7、IE8、Firefox 3.5.5、Google Chrome 3.0、Safari 4.0.4、Opera 10.00 中进行了测试。
它作为PutCursorAtEnd 插件在jquery.com上可用。为方便起见,1.0 版的代码如下:
// jQuery plugin: PutCursorAtEnd 1.0
// http://plugins.jquery.com/project/PutCursorAtEnd
// by teedyay
//
// Puts the cursor at the end of a textbox/ textarea
// codesnippet: 691e18b1-f4f9-41b4-8fe8-bc8ee51b48d4
(function($)
{
    jQuery.fn.putCursorAtEnd = function()
    {
    return this.each(function()
    {
        $(this).focus()
        // If this function exists...
        if (this.setSelectionRange)
        {
        // ... then use it
        // (Doesn't work in IE)
        // Double the length because Opera is inconsistent about whether a carriage return is one character or two. Sigh.
        var len = $(this).val().length * 2;
        this.setSelectionRange(len, len);
        }
        else
        {
        // ... otherwise replace the contents with itself
        // (Doesn't work in Google Chrome)
        $(this).val($(this).val());
        }
        // Scroll to the bottom, in case we're in a tall textarea
        // (Necessary for Firefox and Google Chrome)
        this.scrollTop = 999999;
    });
    };
})(jQuery);
<script type="text/javascript">  
    function SetEnd(txt) {  
      if (txt.createTextRange) {  
       //IE  
       var FieldRange = txt.createTextRange();  
       FieldRange.moveStart('character', txt.value.length);  
       FieldRange.collapse();  
       FieldRange.select();  
       }  
      else {  
       //Firefox and Opera  
       txt.focus();  
       var length = txt.value.length;  
       txt.setSelectionRange(length, length);  
      }  
    }   
</script>  
此功能在 IE9、Firefox 6.x 和 Opera 11.x 中对我有效