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

jquery focus text box and expanded text box focus method_jquery

WBOY
Release: 2016-05-16 17:49:13
Original
1154 people have browsed it
The cursor focused position is at the front
Copy the code The code is as follows:




jquery focused text box-Script Home









jquery extended text box focus method

In different browsers, a text box, if only If you set focus() directly to the text box, the cursor focus position may be at the front. The following code extends jquery with a textFocus method to focus the text box and put the cursor at the end, using $("input").textFocus(). You can also pass in a numeric parameter to set the position of the cursor focus. For example, $("input").textFocus(2), the cursor is after the second character.
Copy code The code is as follows:

(function($){
$.fn .textFocus=function(v){
var range,len,v=v===undefined?0:parseInt(v);
this.each(function(){
if($.browser .msie){
range=this.createTextRange(); //Text box creation range
v===0?range.collapse(false):range.move("character",v); // Range folding
range.select(); //Select
}else{
len=this.value.length;
v===0?this.setSelectionRange(len,len):this .setSelectionRange(v,v); //dom sets the selection directly, and then focus
}
this.focus();
});
return this;
}
} )(jQuery)
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!