Javascript encapsulated textarea operation method set (compatible with good) _ javascript skills

Source: Internet
Author: User
For details about how to process textarea, refer. Well-controlled code encapsulation. 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.

The Code is as follows:


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 +'
';
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 +'
';
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;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.