JS Operation TextArea Method Collection encapsulation (compatible Ie,firefox) _javascript tips

Source: Internet
Author: User
Note: When adding a string to Firefox, there is a bug that scrolltop will be equal to 0, of course, but not perfect. If a master has also studied, trouble pointing.

Complete test Code:

Copy Code code as follows:

<textarea id= "Testlujun" style= "width:300px; height:50px; " >abcdefghijklmnopqrstuvwxyz</textarea>
<br/>
<input onclick= "alert (tt.getcursorposition (test)" type= "button" value= "cursor position"/>
<input onclick= "tt.setcursorposition (test,3)" type= "button" value= "set cursor to 3 position"/>
<input onclick= "Tt.add" (Test, ' hello ') "type=" button "value=" Add ' hello ' to cursor behind "/>
<input onclick= "Tt.del (test,2)" type= "button" value= "delete cursor before 2 characters"/>
<input onclick= "Tt.del (test,-2)" type= "button" value= "delete cursor after 2 characters"/>
<input onclick= "Tt.sel (test,0,2)" type= "button" value= "position of selected character 0-2"/>
<input onclick= "tt.selstring (Test, ' MnO ')" type= "button" value= "Location of selected character ' MnO '"/>
<script type= "Text/javascript" >//<! [cdata[
var test = document.getElementById (' Testlujun ');
var TT = {

/*
* Get 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 cursor position
* @Method setcursorposition
* @param t element
* @param p number
* @return
*/
Setcursorposition:function (t, p) {
This.sel (T,P,P);
},

/*
* Inserted behind 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 the n characters in front of or behind the cursor
* @Method del
* @param t element
* @param n number n>0 back n<0 front
* @return
* ScrollTop value will be cleared when reset value is 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 position
* @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;
}

}
]]></script>

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.