Functions and usage of the extension for text fields:
1. Obtain the cursor position: $ (elem). iGetFieldPos ();
2. Set the cursor position: $ (elem). iSelectField (start );
3. Select the characters in the specified position: $ (elem). iSelectField (start, end );
4. Select the specified character: $ (elem). iSelectStr (str );
5. Insert a string after the cursor: $ (elem). iAdd (str );
6. Delete n characters before (-n) or after (n): $ (elem). iDel (n );
JQuery extension code:
Copy codeThe Code is as follows:
; (Function ($ ){
/*
* JQuery extension for text field cursor operations (selection, addition, deletion, and removal)
*/
$. Fn. extend ({
/*
* Obtain the cursor position
*/
IGetFieldPos: function (){
Var field = this. get (0 );
If (document. selection ){
// IE
$ (This). focus ();
Var sel = document. selection;
Var range = sel. createRange ();
Var dupRange = range. duplicate ();
DupRange. moveToElementText (field );
DupRange. setEndPoint ('endtoend', range );
Field. selectionStart = dupRange. text. length-range.text.length;
Field. selectionEnd = field. selectionStart + range. text. length;
}
Return field. selectionStart;
},
/*
* Select characters in the specified position | set the cursor position
* --- Select from start (including start) to end (excluding end)
* --- If the end value is not entered, the cursor position is set (after the start character)
*/
ISelectField: function (start, end ){
Var field = this. get (0 );
// If the end is not defined, the cursor position is set.
If (arguments [1] = undefined ){
End = start;
}
If (document. selection ){
// IE
Var range = field. createTextRange ();
Range. moveEnd ('character ',-$ (this). val (). length );
Range. moveEnd ('character ', end );
Range. moveStart ('character ', start );
Range. select ();
} Else {
// Non-IE
Field. setSelectionRange (start, end );
$ (This). focus ();
}
},
/*
* Select the specified string
*/
ISelectStr: function (str ){
Var field = this. get (0 );
Var I = $ (this). val (). indexOf (str );
I! =-1? $ (This). iSelectField (I, I + str. length): false;
},
/*
* Insert a string after the cursor
*/
IAddField: function (str ){
Var field = this. get (0 );
Var v = $ (this). val ();
Var len = $ (this). val (). length;
If (document. selection ){
// IE
$ (This). focus ()
Document. selection. createRange (). text = str;
} Else {
// Non-IE
Var selPos = field. selectionStart;
$ (This ). val ($ (this ). val (). slice (0, field. selectionStart) + str + $ (this ). val (). slice (field. selectionStart, len ));
This. iSelectField (selPos + str. length );
};
},
/*
* Delete n characters before (-) or after (+) the cursor.
*/
IDelField: function (n ){
Var field = this. get (0 );
Var pos = $ (this). iGetFieldPos ();
Var v = $ (this). val ();
// If the value is greater than 0, the backend is deleted. If the value is smaller than 0, the front is deleted.
$ (This). val (n> 0? V. slice (0, pos-n) + v. slice (pos): v. slice (0, pos) + v. slice (pos-n ));
$ (This). iSelectField (pos-(n <0? 0: n ));
}
});
}) (JQuery );
Load the code to the extension, and then call it according to the method name in the extension.