| ;(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); |