The cursor position on focus is wrong does not default to 0 position it go开发者_JAVA技巧es to wherever I click it does anyone have any suggestions?
$('input[type="text"][name="name"]').focus(function() {
$(this).setCursorPosition(0);
}
Would something like the above work I can't see anything in my code thats causing this.
It's because .setCursorPosition is not a classic jQuery method. There are, however plugins for this.
new function($) {
$.fn.setCursorPosition = function(pos) {
if ($(this).get(0).setSelectionRange) {
$(this).get(0).setSelectionRange(pos, pos);
} else if ($(this).get(0).createTextRange) {
var range = $(this).get(0).createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
}(jQuery);
Taken here
精彩评论