2010-06-09 43 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(); 
    }); 
});