开发者

Show beginning of HTML input value on blur with JavaScript

开发者 https://www.devze.com 2023-01-08 23:53 出处:网络
I have a fixed width input of type text.If the user was to enter a long value into this field I would like the beginning of the value to be shown when the input no longer has focus.

I have a fixed width input of type text. If the user was to enter a long value into this field I would like the beginning of the value to be shown when the input no longer has focus.

Default behaviour in FF and IE leaves the view of the field value in the same state. Chrome on the other hand seems to behave as I want, it shows the beginning of the value.

I have tried to reset the caret position in IE through the textRange object's Select() method but this seems to re-invoke the blur event resulting in recursive chain (not sure why, but that's a s开发者_运维技巧eparate issue).

Does anyone have any suggestions? There must be an easy way to do this?


I rigged this code together, it works on IE8. The setTimeout of 1ms is because IE automatically shows the second value if a textbox's value is set twice in a row(surprising):

function resetPosition(element){
    var v = element.value;
    element.value="";
    setTimeout(function(){element.value = v;},1);
}

Also tested on IE7, and unobtrusive on Chrome(which does it automatically).


You can call element.setSelectionRange(0, 0) to set the position.

0

精彩评论

暂无评论...
验证码 换一张
取 消