获取光标在文本框的位置、设置光标至文本框末尾
2014-02-07 16:53
363 查看
// 获取光标在文本框的位置 function _getFocus(elem) { var index = 0; if (document.selection) {// IE Support elem.focus(); var Sel = document.selection.createRange(); if (elem.nodeName === 'TEXTAREA') {//textarea var Sel2 = Sel.duplicate(); Sel2.moveToElementText(elem); var index = -1; while (Sel2.inRange(Sel)) { Sel2.moveStart('character'); index++; }; } else if (elem.nodeName === 'INPUT') {// input Sel.moveStart('character', -elem.value.length); index = Sel.text.length; } } else if (elem.selectionStart || elem.selectionStart == '0') { // Firefox support index = elem.selectionStart; } return (index); } //光标移动至末尾 function moveEnd(obj) { obj.focus(); var len = obj.value.length; if (document.selection) { var sel = obj.createTextRange(); sel.moveStart('character', len); sel.collapse(); sel.select(); } else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') { obj.selectionStart = obj.selectionEnd = len; } }
相关文章推荐
- javascript中获取和设置文本框中光标的位置
- js获取和设置文本框光标的位置
- js获取光标位置和设置文本框光标位置示例代码
- 设置和获取文本框中的光标位置
- js获取文本框(或文本域)光标位置以及设置光标位置
- js获取光标位置和设置文本框光标位置
- [图解]在输入框和文本框中获取和设置光标位置,以及选中文本和获取选中文本值的方法 --- 详解,兼容所有浏览器。
- 设置/获取光标在文本框的位置
- js获取光标位置和设置文本框光标位置示例代码
- js获取和设置文本框光标的位置
- IE 6 7 8获取文本框中光标的位置
- javascript获取以及设置光标位置
- Android - 小功能 - EditText设置/隐藏光标位置、选中文本和获取/清除焦点
- javascript获取以及设置光标位置
- js 获取文本框中光标所在字符串索引位置
- JavaScript获取文本框光标的像素位置
- [转]JS 获取、设置 输入框光标位置
- 获取/设置input中的鼠标光标位置