The jQuery-based input box inserts content at the cursor position and selects the implementation code of the function. For more information, see.
The Code is as follows:
//
// Usage
// $ (Text field selector). insertContent ("inserted content ");
// $ (Text field selector ). insertContent ("inserted content", numeric value); // select the boundary between the two sides of the inserted text content based on the value. The value 0 indicates that all the inserted text is selected, -1 indicates that only one character is selected for each inserted text.
//
// Insert content at the cursor position and select
(Function ($ ){
$. Fn. extend ({
InsertContent: function (myValue, t ){
Var $ t = $ (this) [0];
If (document. selection) {// ie
This. focus ();
Var sel = document. selection. createRange ();
Sel. text = myValue;
This. focus ();
Sel. moveStart ('character ',-l );
Var wee = sel. text. length;
If (arguments. length = 2 ){
Var l = $ t. value. length;
Sel. moveEnd ("character", wee + t );
T <= 0? Sel. moveStart ("character", wee-2 * t-myValue. length): sel. moveStart ("character", wee-t-myValue. length );
Sel. select ();
}
} Else if ($ t. selectionStart | $ t. selectionStart = '0 '){
Var startPos = $ t. selectionStart;
Var endPos = $ t. selectionEnd;
Var scrollTop = $ t. scrollTop;
$ T. value = $ t. value. substring (0, startPos) + myValue + $ t. value. substring (endPos, $ t. value. length );
This. focus ();
$ T. selectionStart = startPos + myValue. length;
$ T. selectionEnd = startPos + myValue. length;
$ T. scrollTop = scrollTop;
If (arguments. length = 2 ){
$ T. setSelectionRange (startPos-t, $ t. selectionEnd + t );
This. focus ();
}
}
Else {
This. value + = myValue;
This. focus ();
}
}
})
}) (JQuery );