2010-06-09 26 views
1

我不知道这是什么,所以它很难谷歌。Javascript/jquery - 重置文本框查看区域后标签

如果您有一个简单的html输入类型=文本宽度小(例如20),然后键入的字符数多于可以显示的字符数,则会显示您输入的最后一个字符。而第一批被滚出视:

This is my long text // this is the whole string 
my long string // this is what is actually visible in the input box 

如何让这个当你打标签,视图区域重置使得字符串的开始是可见的,到底是隐藏?

my long string // this is what is visible in the input box when typing 
This is my // this is what is I want to be visible in the input box after you hit tab 

你是怎么称呼这个的,你是怎么做到的?

+0

对不起@JK,我失败了。我已经删除了我的答案。即使如此,你也许会想重新提问,因为这个问题现在可能有点陈旧了。 – griegs 2010-06-10 01:02:18

回答

0

我不知道你的问题是否已经回答,但我只是想回答自己。你可以通过使用范围和窗口选择来完成。 Here, I made a JSFiddle for you.

$(document).ready(function(){ 
    $("input").bind("keyup",function(e){ 
    if(e.keyCode==9){ 
     var r=document.createRange(), 
     gs=getSelection(); 
     r.setStart(this,0);r.setEnd(this,0); 
     gs.removeAllRanges();gs.addRange(r); 
    } 
    }); 
    $("input").bind("blur"),function(){ 
    $(this).focus(); 
    }); 
});