[前端] 定位光标位置
来源:互联网 发布:女神威捏脸数据 编辑:程序博客网 时间:2024/05/19 00:12
当input框获取焦点的时候,改变光标的位置
方法一:
function setSelectionRange(input, selectionStart, selectionEnd) { if (input.setSelectionRange) { input.focus(); input.setSelectionRange(selectionStart, selectionEnd); } else if (input.createTextRange) { var range = input.createTextRange(); range.collapse(true); range.moveEnd('character', selectionEnd); range.moveStart('character', selectionStart); range.select(); }}function setCaretToPos (input, pos) { setSelectionRange(input, pos, pos);}// 调用setCaretToPos(document.getElementById("input"), 4);
方法二:
$.fn.selectRange = function(start, end) { return this.each(function() { if (this.setSelectionRange) { this.focus(); this.setSelectionRange(start, end); } else if (this.createTextRange) { var range = this.createTextRange(); range.collapse(true); range.moveEnd('character', end); range.moveStart('character', start); range.select(); } });};// 调用$('#ele').selectRange(3,5);
方法三:
$.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(){ this.setCursorPosition(this.val().length);}// 调用$(ele).focusEnd();
谢谢关注!
0 0
- [前端] 定位光标位置
- // 定位光标位置
- JavaScript获取光标位置、定位光标位置
- 光标定位和获取当前光标位置
- editView光标的位置定位
- JS定位光标在textarea中的位置
- js实现文本内容光标定位,获取光标位置
- Word2007鼠标失效 不能定位光标位置 解决办法(转)
- 怎样把光标定位到TextField文本的末尾位置
- js定位光标到输入框指定位置
- Eclipse java 文件编辑器 根据字符串定位光标位置
- VIM 打开文件,光标定位到上次退出的位置
- 光标定位
- 光标定位
- 光标定位
- 光标定位
- 光标位置
- Javascript设置和获取Textarea的光标位置的方法(亲测有用),可定位光标到某个位置
- PCM与DSD究竟是什么??
- 简明20世纪东亚史--刘仲敬
- 使用Sqlserver事务发布实现数据同步
- PAT(甲级)1089
- [R语言]快速安装用户自定义词典——用于分词
- [前端] 定位光标位置
- tomcat性能调优
- 谈业务流程全生命周期管理支撑业务流程再造(3)
- 城市天气三小时预报,天气预报接口实现
- Android 回调实例详解
- PAT(甲级)1090
- 青少年如何使用 Python 开始游戏开发
- jQuery.validate验证上传文件大小
- Android如何调用支付宝支付 微信支付