1 /** 2 * 输入框光标位置相关处理 3 * @constructor 4 * @name vui.inputCursor 5 * @author dong 6 */ 7 vui.inputCursor = { 8 /** @lends vui.inputCursor */ 9 /** 10 * getSelection 11 * @name vui.inputCursor.getSelection 12 * @param {Object} el 主对象 13 * @see <a href="http://stackoverflow.com/questions/235411/is-there-an-internet-explorer-approved-substitute-for-selectionstart-and-selecti">stackoverflow</a> 14 */ 15 getSelection: function(el){ 16 var start = 0, end = 0, normalizedValue, range, 17 textInputRange, len, endRange; 18 19 if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") { 20 start = el.selectionStart; 21 end = el.selectionEnd; 22 } else { 23 range = document.selection.createRange(); 24 25 if (range && range.parentElement() == el) { 26 len = el.value.length; 27 normalizedValue = el.value.replace(/\r\n/g, "\n"); 28 29 // Create a working TextRange that lives only in the input 30 textInputRange = el.createTextRange(); 31 textInputRange.moveToBookmark(range.getBookmark()); 32 33 // Check if the start and end of the selection are at the very end 34 // of the input, since moveStart/moveEnd doesn't return what we want 35 // in those cases 36 endRange = el.createTextRange(); 37 endRange.collapse(false); 38 39 if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) { 40 start = end = len; 41 } else { 42 start = -textInputRange.moveStart("character", -len); 43 start += normalizedValue.slice(0, start).split("\n").length - 1; 44 45 if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) { 46 end = len; 47 } else { 48 end = -textInputRange.moveEnd("character", -len); 49 end += normalizedValue.slice(0, end).split("\n").length - 1; 50 } 51 } 52 } 53 } 54 55 return { 56 start: start, 57 end: end 58 }; 59 }, 60 /** 61 * 获取光标的起始位置 62 * @name vui.inputCursor.getStartPos 63 * @param {Object} obj 主对象 64 */ 65 getStartPos: function(obj){ 66 return this.getSelection(obj).start; 67 }, 68 /** 69 * 获取光标的结束位置 70 * @name vui.inputCursor.getEndPos 71 * @param {Object} obj 主对象 72 */ 73 getEndPos: function(obj){ 74 return this.getSelection(obj).end; 75 }, 76 /** 77 * 光标处插入字符 78 * @name vui.inputCursor.insert 79 * @param {Object} obj 主对象 80 * @param {String} str 插入的字符 81 */ 82 insert: function(obj, str){ 83 if (document.selection) { 84 //IE support 85 obj.focus(); 86 sel = document.selection.createRange(); 87 sel.text = str; 88 obj.focus(); 89 }else if (obj.selectionStart || obj.selectionStart == '0') { 90 //MOZILLA/NETSCAPE support 91 startPos = obj.selectionStart; 92 endPos = obj.selectionEnd; 93 scrollTop = obj.scrollTop; 94 obj.value = obj.value.substring(0, startPos) + str + obj.value.substring(endPos,obj.value.length); 95 obj.focus(); 96 obj.selectionStart = startPos + str.length; 97 obj.selectionEnd = startPos + str.length; 98 obj.scrollTop = scrollTop; 99 } else { 100 obj.value += str; 101 obj.focus(); 102 } 103 }, 104 /** 105 * 选中指定区间 106 * @name vui.inputCursor.select 107 * @param {Object} obj 主对象 108 * @param {Number} start 开始位置 109 * @param {Number} end 结束位置 110 */ 111 select: function(obj, start, end){ 112 if(typeof end == 'undefined' || end < start) end = start; 113 obj.focus(); 114 if(document.selection){ 115 var range = obj.createTextRange(); 116 range.move('character', start); 117 range.moveEnd('character', end); 118 range.select(); 119 } 120 else{ 121 obj.selectionStart = start; 122 obj.selectionEnd = end; 123 } 124 }, 125 /** 126 * 替换指定区域的文字 127 * @name vui.inputCursor.replace 128 * @param {Object} obj 主对象 129 * @param {Number} start 开始位置 130 * @param {Number} end 结束位置 131 * @param {String} text 替换指定区域的文字 132 * @param {Boolean} focus true 替换指定区域的文字 133 */ 134 replace: function(obj, start, end, text, focus){ 135 obj.value = obj.value.substr(0, start) + text + obj.value.substr(end, obj.value.length); 136 if(focus){ 137 this.select(obj, start + text.length); 138 } 139 } 140 }; 141 142