您的位置:首页 > 其它

输入框文本选择及焦点定位

2012-04-10 16:12 183 查看
/**

* 设置鼠标位置

*

*/

$.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;

var 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(){

this.setCursorPosition(this.val().length);

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: