Although it seems useless for you now, it is better to collect information when you use it.
It is troublesome to operate the characters in textarea In the DOM.
So I have shared this encapsulation with you and tested IE6, 8, firefox, chrome, opera, and safari. Compatible.
Note: When you add a string in firefox, the bug is that scrollTop will be equal to 0. Of course, the problem is solved, but it is not perfect. If you have already studied it, give me some advice.Copy codeCode: var TT = {
/*
* Obtain the cursor position
* @ Method getCursorPosition
* @ Param t element
* @ Return number
*/
GetCursorPosition: function (t ){
If (document. selection ){
T. focus ();
Var ds = document. selection;
Var range = ds. createRange ();
Var stored_range = range. duplicate ();
Stored_range.moveToElementText (t );
Stored_range.setEndPoint ("EndToEnd", range );
T. selectionStart = stored_range.text.length-range. text. length;
T. selectionEnd = t. selectionStart + range. text. length;
Return t. selectionStart;
} Else return t. selectionStart
},
/*
* Set the cursor position
* @ Method setCursorPosition
* @ Param t element
* @ Param p number
* @ Return
*/
SetCursorPosition: function (t, p ){
This. sel (t, p, p );
},
/*
* Insert to the end of the cursor
* @ Method add
* @ Param t element
* @ Param txt String
* @ Return
*/
Add: function (t, txt ){
Var val = t. value;
If (document. selection ){
T. focus ()
Document. selection. createRange (). text = txt;
} Else {
Var cp = t. selectionStart;
Var ubbLength = t. value. length;
Var s = t. scrollTop;
// Document. getElementById ('aaa'). innerHTML + = s + '<br/> ';
T. value = t. value. slice (0, t. selectionStart) + txt + t. value. slice (t. selectionStart, ubbLength );
This. setCursorPosition (t, cp + txt. length );
// Document. getElementById ('aaa'). innerHTML + = t. scrollTop + '<br/> ';
Firefox = navigator. userAgent. toLowerCase (). match (/firefox \/([\ d \.] +)/) & setTimeout (function (){
If (t. scrollTop! = S) t. scrollTop = s;
}, 0)
};
},
/*
* Delete n characters before or after the cursor
* @ Method del
* @ Param t element
* @ Param n number n> 0 followed by n <0
* @ Return
* When the value is reset, The scrollTop value is cleared by 0.
*/
Del: function (t, n ){
Var p = this. getCursorPosition (t );
Var s = t. scrollTop;
Var val = t. value;
T. value = n> 0? Val. slice (0, p-n) + val. slice (p ):
Val. slice (0, p) + val. slice (p-n );
This. setCursorPosition (t, p-(n <0? 0: n ));
Firefox = navigator. userAgent. toLowerCase (). match (/firefox \/([\ d \.] +)/) & setTimeout (function (){
If (t. scrollTop! = S) t. scrollTop = s;
}, 10)
},
/*
* Select the text from s to z
* @ Method sel
* @ Param t element
* @ Param s number
* @ Param z number
* @ Return
*/
Sel: function (t, s, z ){
If (document. selection ){
Var range = t. createTextRange ();
Range. moveEnd ('character ',-t. value. length );
Range. moveEnd ('character ', z );
Range. moveStart ('character ', s );
Range. select ();
} Else {
T. setSelectionRange (s, z );
T. focus ();
}
},
/*
* Select a string
* @ Method sel
* @ Param t element
* @ Param s String
* @ Return
*/
SelString: function (t, s ){
Var index = t. value. indexOf (s );
Index! =-1? This. sel (t, index, index + s. length): false;
}
}