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

筅森魡賤 提交于 2019-12-11 13:45:39

问题


So here's what I'm working on:

http://jsfiddle.net/hn8EY/

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

This is working fine for physically setting the position of the cursor, but I also want it to be where the viewpoint of the user jumps to that point, so that the current position of the cursor is actually shown. I don't know if there is any solution to this.


回答1:


Try setting the 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



来源:https://stackoverflow.com/questions/20252449/jquery-focus-textbox-and-go-to-end-but-also-set-the-position-of-the-cursor-so-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!