jquery Chinese Domain cursor action (check, add, delete, get)

Source: Internet
Author: User

The functionality and usage for the extension of the text field:
1, get the cursor position: $ (elem). Igetfieldpos ();
2, set the cursor position: $ (elem). Iselectfield (start);
3. Select the character in the specified position: $ (elem). Iselectfield (Start,end);
4. Select the specified character: $ (elem). ISELECTSTR (str);
5. Insert string after cursor: $ (elem). Iadd (str);
6. Delete the n characters in front of the cursor (-N) or after (n): $ (elem). Idel (n);

The code is as follows Copy Code

;(function ($) {
/*
* Text field cursor operation (SELECT, add, delete, take)
*/
$.fn.extend ({
/*
* Get the cursor location
*/
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 the character in the specified position | | Set cursor position
*---is selected from Start (with start) and ends at end (excluding the first)
*---If you do not enter the end value, that is to set the position of the cursor (first word end-of-file)
*/
Iselectfield:function (start,end) {
var field=this.get (0);
End is undefined, set the cursor position
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;
},
/*
* Inserts 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 in front (-) or back (+) of the cursor
*/
Idelfield:function (n) {
var field=this.get (0);
var pos=$ (this). Igetfieldpos ();
var v=$ (this). Val ();
Greater than 0 deletes the following, less than 0 deletes the preceding
$ (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);

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.