jQuery聚焦文本框并结束,但是还可以设置光标的位置,以便用户可以看到它? [英] jQuery focus textbox and go to end, but ALSO set the position of the cursor so user can see it?

查看:108
本文介绍了jQuery聚焦文本框并结束,但是还可以设置光标的位置,以便用户可以看到它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我正在从事的工作:

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.

推荐答案

尝试设置scrollLeft:

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

这篇关于jQuery聚焦文本框并结束,但是还可以设置光标的位置,以便用户可以看到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆