Home > Web Front-end > JS Tutorial > body text

jQuery extension sharing for text field cursor operations_jquery

WBOY
Release: 2016-05-16 16:56:04
Original
941 people have browsed it

The functions and usage of this extension for text fields:
1. Get 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 characters: $(elem).iSelectStr(str);
5. Insert a string after the cursor: $(elem).iAdd(str);
6. Delete n characters before (-n) or after (n) the cursor: $(elem).iDel(n) ;

jQuery extension code:

Copy code The code is as follows:

;(function($){
/*
* jQuery extension for text field cursor operations (select, add, delete, take)
*/
$.fn. extend({
                                                                                                                        /* if(document. selection){
                                                                                                                                                                                                         . );
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 characters in the specified position || Set the cursor position
                                                                                                                                         but                                                      After the start character)
*/
iSelectField:function(start,end){
var field=this.get(0);
//end is not defined, then 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); start);
range.select();
}else{
//Non-IE
field.setSelectionRange(start,end);
            $(this).focus();
                                                                                          ​​=$( this).val().indexOf(str);
i != -1 ? $(this).iSelectField(i,i str.length) : false;
},
/*
          * Insert string
after the cursor                                                                                  */ ;
var len=$(this).val().length;
if(document.selection){
                                            document. selection.createRange().text=str;
                                                                                                                                                   $(this).val( ).slice(0,field.selectionStart) str $(this).val().slice(field.selectionStart,len));
this.iSelectField(selPos str.length);
};
                                                                                                                                                                                                                                            (0);
var pos=$(this).iGetFieldPos();
var v=$(this).val();

                  $(this).val(n>0 ? v.slice(0,pos-n) v.slice(pos) : v.slice(0,pos) v.slice(pos-n));
                                                                                                                               
Load it in the extension code, and then call it according to the method name in the extension.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template