惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
罗磊的独立博客
量子位
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
M
MIT News - Artificial intelligence
月光博客
月光博客
IT之家
IT之家
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
D
Docker
The GitHub Blog
The GitHub Blog
B
Blog
V
Visual Studio Blog
博客园 - Franky
N
Netflix TechBlog - Medium
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
博客园 - 聂微东
U
Unit 42

博客园 - 三聪

流操作 如何利用.Net内置类,解析未知复杂Json对象 制作等比环切缩缩图 让window.setTimeout等支持带参数方法 利用NewID()生成随机数 创建缩略图 winform给html提供接口 ie下取得iframe里面内容 短网址计算 树冲突 截图片 学习Objective-C: 入门教程 C#根据字节数截取字符串 asp.net本地和全局资料文件的读取方法 jquery实现点击空白的事件 Asp.net 利用Jquery Ajax传送和接收DataTable 用一个最简单方法解决asp.net页面刷新导致数据的重复提交 SOS"未将对象引用设置到对象的实例" .NET调PHP Web Service的典型例子
IE下记住文本框的光标位置
三聪 · 2011-06-14 · via 博客园 - 三聪

方法一:

 1 //#region --文本框TextArea光标相关
 2 var Pos = {
 3     //IE版本号
 4     getIEVer: function() {
 5         var match = navigator.appVersion.match(/MSIE\s+\d+.0;/);
 6         if (match == nullreturn -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();

是不是相当简单