开发者

cursor position on focus

开发者 https://www.devze.com 2023-04-12 02:01 出处:网络
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?

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

0

精彩评论

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

关注公众号