JQuery extension for text field cursor operations _ jquery-js tutorial

Source: Internet
Author: User
In recent projects, methods for operating text fields are used several times, such as the cursor position and deleting the pre-cursor characters. Every time I read the materials (this part of the js method involved is relatively uncommon), it takes a lot of time and effort. Therefore, a jQuery extension is encapsulated to implement the extension of the text domain and the usage method:
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:

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

Related Article

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.