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.
<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>