javascript获取以及设置光标位置
来源:互联网 发布:javascript重定向页面 编辑:程序博客网 时间:2024/05/21 09:02
转载:点击打开链接
一. 获取光标位置:
// 获取光标位置function getCursortPosition (textDom) { var cursorPos = 0; if (document.selection) { // IE Support textDom.focus (); var selectRange = document.selection.createRange(); selectRange.moveStart ('character', -textDom.value.length); cursorPos = selectRange.text.length; }else if (textDom.selectionStart || textDom.selectionStart == '0') { // Firefox support cursorPos = textDom.selectionStart; } return cursorPos;}
二. 设置光标位置:
// 设置光标位置function setCaretPosition(textDom, pos){ if(textDom.setSelectionRange) { // IE Support textDom.focus(); textDom.setSelectionRange(pos, pos); }else if (textDom.createTextRange) { // Firefox support var range = textDom.createTextRange(); range.collapse(true); range.moveEnd('character', pos); range.moveStart('character', pos); range.select(); }}
三. 获取选中文字:
// 获取选中文字function getSelectText() { var userSelection, text; if (window.getSelection) { // Firefox support userSelection = window.getSelection(); } else if (document.selection) { // IE Support userSelection = document.selection.createRange(); } if (!(text = userSelection.text)) { text = userSelection; } return text;}
四. 选中特定范围的文本:
/*** 选中特定范围的文本* 参数:* textDom [JavaScript DOM String] 当前对象* startPos [Int] 起始位置* endPos [Int] 终点位置*/function setSelectText(textDom, startPos, endPos) { var startPos = parseInt(startPos), endPos = parseInt(endPos), textLength = textDom.value.length; if(textLength){ if(!startPos){ startPos = 0; } if(!endPos){ endPos = textLength; } if(startPos > textLength){ startPos = textLength; } if(endPos > textLength){ endPos = textLength; } if(startPos < 0){ startPos = textLength + startPos; } if(endPos < 0){ endPos = textLength + endPos; } if(textDom.createTextRange){ // IE Support var range = textDom.createTextRange(); range.moveStart("character",-textLength); range.moveEnd("character",-textLength); range.moveStart("character", startPos); range.moveEnd("character",endPos); range.select(); }else{ // Firefox support textDom.setSelectionRange(startPos, endPos); textDom.focus(); } }}
五. 在光标后插入文本:
/*** 在光标后插入文本* 参数:* textDom [JavaScript DOM String] 当前对象* value [String] 要插入的文本*/function insertAfterText(textDom, value) { var selectRange; if (document.selection) { // IE Support textDom.focus(); selectRange = document.selection.createRange(); selectRange.text = value; textDom.focus(); }else if (textDom.selectionStart || textDom.selectionStart == '0') { // Firefox support var startPos = textDom.selectionStart; var endPos = textDom.selectionEnd; var scrollTop = textDom.scrollTop; textDom.value = textDom.value.substring(0, startPos) + value + textDom.value.substring(endPos, textDom.value.length); textDom.focus(); textDom.selectionStart = startPos + value.length; textDom.selectionEnd = startPos + value.length; textDom.scrollTop = scrollTop; } else { textDom.value += value; textDom.focus(); }}
阅读全文
0 2
- javascript获取光标位置以及设置光标位置
- javascript获取光标位置以及设置光标位置(兼容firefox)
- javascript获取以及设置光标位置
- javascript获取以及设置光标位置
- javascript获取光标位置
- JavaScript获取光标位置、定位光标位置
- Javascript设置和获取Textarea的光标位置的方法
- javascript获取/设置 文本框/文本域中的光标位置
- JavaScript 获取/设置光标位置,兼容Input&&TextArea
- javascript中获取和设置文本框中光标的位置
- JavaScript获取光标的位置
- javascript 设置焦点 光标位置
- js获取,设置光标位置
- 自定义键盘中的删除键TextField上光标位置的获取以及设置光标的位子
- js获取光标位置和设置文本框光标位置
- Javascript设置和获取Textarea的光标位置的方法(亲测有用),可定位光标到某个位置
- JavaScript获取文本框光标的像素位置
- JavaScript获取文本框光标的像素位置
- Sub-1GHz
- EAS8.0,EAS8.2,WAF2,JS,打开新页面,打开新窗口,传递参数
- Deep CORAL: Correlation Alignment for Deep Domain Adaptation(2016)
- Linux下比较常用的svn命令
- C#中AppDomain.CurrentDomain.BaseDirectory与Application.StartupPath的区别
- javascript获取以及设置光标位置
- Socket TCP/IP UDP、Http、Socket的区别
- 你应该知道的PHP库
- 一个c/c++程序是怎么从代码到可执行文件的
- [bigdata-092] 一个基于eclipse的drools插件的java的drools应用
- TCP/IP网络知识点总结
- Windows上基于solr配置IK(IKAnalyzer)分词器方法
- stl->set/multiset
- linux 安装mysql