jQuery聚焦文本框并转到末尾,但也可以设置光标的位置,以便用户可以看到它

jQuery focus textbox and go to end, but ALSO set the position of the cursor so user can see it?

本文关键字:位置 置光标 用户 文本 聚焦 jQuery 也可以      更新时间:2023-11-25

所以我正在做的是:

http://jsfiddle.net/hn8EY/

$("input").mouseup(function(){
    this.selectionStart = this.value.length;
    this.selectionEnd = this.value.length;
});

这对于物理设置光标的位置很好,但我也希望它位于用户的视点跳到该点的位置,以便实际显示光标的当前位置。我不知道是否有解决办法。

尝试设置scrollLeft:

$("input").mouseup(function(){
    var valLength = this.value.length;
    this.setSelectionRange(valLength , valLength ); //or this.selectionStart = this.value.length; this.selectionEnd = this.value.length;
    //or just use this.selectionStart = this.value.length;
    this.scrollLeft = this.scrollWidth;
});

Fiddle