遇到將tirm完的新字串重設回Textbox時,
Cursor的位置無法停留至文字的最後位置,
網路上的範例也蠻多的,以下範例是改自參考的網址測試
IE 8,IE 9,Chrome,FF可正常運作。
有其他需求的話可參考jQuery的caret plugin。
(function($) {
$.fn.setCursorPos = function(pos){
if(this.length == 0)
return this;
return $(this).setCaret(pos, pos);
};
$.fn.setCaret = function(start, end) {
if(this.length == 0)
return this;
input = this[0];
if (input.createTextRange) {
//IE Only
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
} else if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(start, end);
}
return this;
};
$.fn.focusEnd = function(){
var endPos = this.val().length;
this.setCursorPos(endPos);
};
})(jQuery);
Reference:
jQuery Set Cursor Position in Text Area
沒有留言:
張貼留言
留個話吧:)