输入框文本选择及焦点定位
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);
};
* 设置鼠标位置
*
*/
$.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);
};
相关文章推荐
- 怎么让Android 浏览器焦点在文本输入框时不自动放大页面?
- 文本输入框默认不获取焦点
- 防止密码输入框弹出历史记录,输入框光标 onblur 定位在文本最后面
- <<锋利的jQuery>>样例改进利用, html文本输入框得到与失去输入焦点的提示信息显示切换函数
- 安卓:WebView中iframe,焦点字段出现两个文本输入框,位置错误
- HTML学习感想(5)【图像按钮、文件选择输入框、多行文本输入框】
- form表单字段元素对象(三)— —按回车键,焦点移到下一个文本输入框
- iOS自定义键盘和系统键盘切换且文本输入框一直获得焦点
- select点击option获取文本输入框的焦点事件
- Android让文本输入框默认不获取焦点
- 根据选择不同的下拉值出现相对应的文本输入框
- 可编辑div获取焦点位置和选择区域的文本
- 回车后获得焦点事件,回车后,下一个文本输入框可获得焦点,并选中
- Xamarin.Mac中使用emoji表情,以及让输入框获取焦点,并将光标定位到字符串末尾
- css怎么让Android 浏览器焦点在文本输入框时不自动放大页面
- 实现选择radio按钮后添加一个文本输入框的示例
- dojo 创建按钮和文本输入框 tip 提示以及控件焦点 focus
- [7]文本输入框默认不获取焦点
- JavaScript基础 表单内的text文本输入框 获得焦点onfocus 失去焦点onblur
- Android让文本输入框默认不获取焦点