方法一:
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();
是不是相当简单
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述