Home  >  Article  >  Web Front-end  >  Summary of common methods for operating textarea in js_javascript skills

Summary of common methods for operating textarea in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:47:30953browse

It is more troublesome to operate the characters in the textarea in the DOM.
So I have this package to share with everyone. I have tested it on IE6,8, firefox, chrome, opera, and safari. Compatibility is no problem.
Note: There is a bug when adding a string under Firefox, that is, scrollTop will be equal to 0. Of course it is solved, but it is not perfect. If there are experts who have studied it, please give some advice.

Copy code The code is as follows:

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);
},
/*
* Insert after 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 preceded by
* @return
* When resetting the value, the value of scrollTop will be cleared to 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;
}
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn