方法一:
1 //#region --文本框TextArea光标相关
2 var Pos = {
3 //IE版本号
4 getIEVer: function() {
5 var match = navigator.appVersion.match(/MSIE\s+\d+.0;/);
6 if (match == null) return -1;
7 return +match[0].match(/\d+/)[0];
8 },
9 getPos: function(textBox) {
10 if (document.selection) {
11 var range = document.selection.createRange();
12 var range_all = document.body.createTextRange();
13 range_all.moveToElementText(textBox);
14 for (start = 0; range_all.compareEndPoints("StartToStart", range) < 0; start++) {
15 range_all.moveStart('character', 1);
16 }
17 var range_all = document.body.createTextRange();
18 range_all.moveToElementText(textBox);
19 for (end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; end++) {
20 range_all.moveStart('character', 1);
21 }
22 var selectionStart = start;
23 var selectionEnd = end;
24 //ie8-\r\n在move时会当成一个字符
25 if (this.getIEVer() > 8) {
26 var s1 = textBox.value.substr(0, selectionStart);
27 var s2 = textBox.value.substr(0, selectionEnd);
28 var m1 = s1.match(/\r\n/g);
29 var m2 = s2.match(/\r\n/g);
30 if (m1 != null) selectionStart += m1.length;
31 if (m2 != null) selectionEnd += m2.length;
32 }
33 return [selectionStart, selectionEnd];
34 }
35 else {
36 return [t.selectionStart, t.selectionEnd];
37 }
38 },
39 setPos: function(textBox, a, b) {
40 if (a && b) {
41 if (!(document.selection)) {
42 textBox.setSelectionRange(a, b);
43 }
44 else {
45 var range = textBox.createTextRange();
46 range.moveEnd('character', -textBox.value.length);
47 range.moveEnd('character', b);
48 range.moveStart('character', a);
49 range.select();
50 textBox.scrollTop = textBox.scrollTop2;
51
52 }
53 }
54 },
55 insert: function(textBox, value) {
56 var text = textBox.value;
57 var a = textBox.selectionStart2 || textBox.selectionStart;
58 var b = textBox.selectionEnd2 || textBox.selectionEnd;
59 var left = text.substr(0, a);
60 var right = text.substr(b, text.length);
61 b = a + value.length;
62 textBox.value = left + value + right;
63 this.setPos(textBox, b, b);
64 this.savePos(textBox);
65 },
66 savePos: function(textBox) {
67 if (document.selection) {
68 var pos = this.getPos(textBox);
69 textBox.selectionStart2 = pos[0];
70 textBox.selectionEnd2 = pos[1];
71 textBox.scrollTop2 = textBox.scrollTop;
72 }
73 }
74 };
75
76 jQuery.fn.extend({
77 rememberPos: function () {
78 if (document.selection) {
79 this.each(function (i, el) {
80 $(el).keyup(function () { Pos.savePos(el); });
81 $(el).mouseup(function () { Pos.savePos(el); });
82 $(el).focus(function () {
83 Pos.setPos(el, el.selectionStart2, el.selectionEnd2);
84 });
85
86 });
87 }
88 }
89 });
90
91 $(function () {
92 $("textarea").rememberPos();
93 })
2 var Pos = {
3 //IE版本号
4 getIEVer: function() {
5 var match = navigator.appVersion.match(/MSIE\s+\d+.0;/);
6 if (match == null) return -1;
7 return +match[0].match(/\d+/)[0];
8 },
9 getPos: function(textBox) {
10 if (document.selection) {
11 var range = document.selection.createRange();
12 var range_all = document.body.createTextRange();
13 range_all.moveToElementText(textBox);
14 for (start = 0; range_all.compareEndPoints("StartToStart", range) < 0; start++) {
15 range_all.moveStart('character', 1);
16 }
17 var range_all = document.body.createTextRange();
18 range_all.moveToElementText(textBox);
19 for (end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; end++) {
20 range_all.moveStart('character', 1);
21 }
22 var selectionStart = start;
23 var selectionEnd = end;
24 //ie8-\r\n在move时会当成一个字符
25 if (this.getIEVer() > 8) {
26 var s1 = textBox.value.substr(0, selectionStart);
27 var s2 = textBox.value.substr(0, selectionEnd);
28 var m1 = s1.match(/\r\n/g);
29 var m2 = s2.match(/\r\n/g);
30 if (m1 != null) selectionStart += m1.length;
31 if (m2 != null) selectionEnd += m2.length;
32 }
33 return [selectionStart, selectionEnd];
34 }
35 else {
36 return [t.selectionStart, t.selectionEnd];
37 }
38 },
39 setPos: function(textBox, a, b) {
40 if (a && b) {
41 if (!(document.selection)) {
42 textBox.setSelectionRange(a, b);
43 }
44 else {
45 var range = textBox.createTextRange();
46 range.moveEnd('character', -textBox.value.length);
47 range.moveEnd('character', b);
48 range.moveStart('character', a);
49 range.select();
50 textBox.scrollTop = textBox.scrollTop2;
51
52 }
53 }
54 },
55 insert: function(textBox, value) {
56 var text = textBox.value;
57 var a = textBox.selectionStart2 || textBox.selectionStart;
58 var b = textBox.selectionEnd2 || textBox.selectionEnd;
59 var left = text.substr(0, a);
60 var right = text.substr(b, text.length);
61 b = a + value.length;
62 textBox.value = left + value + right;
63 this.setPos(textBox, b, b);
64 this.savePos(textBox);
65 },
66 savePos: function(textBox) {
67 if (document.selection) {
68 var pos = this.getPos(textBox);
69 textBox.selectionStart2 = pos[0];
70 textBox.selectionEnd2 = pos[1];
71 textBox.scrollTop2 = textBox.scrollTop;
72 }
73 }
74 };
75
76 jQuery.fn.extend({
77 rememberPos: function () {
78 if (document.selection) {
79 this.each(function (i, el) {
80 $(el).keyup(function () { Pos.savePos(el); });
81 $(el).mouseup(function () { Pos.savePos(el); });
82 $(el).focus(function () {
83 Pos.setPos(el, el.selectionStart2, el.selectionEnd2);
84 });
85
86 });
87 }
88 }
89 });
90
91 $(function () {
92 $("textarea").rememberPos();
93 })
方法二:
其实这是一个误区,其实我们根本就没有兴趣知道光标的具体位置,我们的目的一般有两个
1.在光标处插入字符
2.记住光标位置,在重获焦点时还原光标位置
所以我们只需要textBox.selectRange=document.selection.createRange().duplicate();将选取区间复制一份保存下来
1.在光标处插入字符textBox.selectRange.text="xxxx";
2.记住光标位置,在重获焦点时还原光标位置textBox.selectRange.select();
是不是相当简单