当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 将鼠标焦点定位到文本框最后(代码分享)

将鼠标焦点定位到文本框最后(代码分享)

2019年03月30日  | 移动技术网IT编程  | 我要评论

经测试,兼容ie8

//设置焦点相关---begin
//用法:$("#txtinput").val("你好").focusend();
$.fn.setcursorposition = function (position) {
 if (this.lengh == 0) return this;
 return $(this).setselection(position, position);
}
$.fn.setselection = function (selectionstart, selectionend) {
 if (this.lengh == 0) return this;
 input = this[0];
 if (input.createtextrange) {
 var range = input.createtextrange();
 range.collapse(true);
 range.moveend('character', selectionend);
 range.movestart('character', selectionstart);
 range.select();
 } else if (input.setselectionrange) {
 input.focus();
 input.setselectionrange(selectionstart, selectionend);
 }
 return this;
}
$.fn.focusend = function () {
 if (this.val() != undefined) {
 this.setcursorposition(this.val().length);
 }
}
//设置焦点相关---end

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网