Jquery focus text box and Extended text box focus Method

Source: Internet
Author: User

The cursor is focused at the beginning.
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> jquery focus text box-script house </title>
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type = "text/javascript"> </script>
</Head>
<Body>
<Form action = "http://www.baidu.com" id = "cse-search-box">
<Div>
<Input type = "hidden" name = "cx" value = "partner-pub-7740261255677392: 7064996710"/>
<Input type = "hidden" name = "ie" value = "UTF-8"/>
<! -- Text box --> <input type = "text" name = "q" size = "25"/>
<Input type = "submit" name = "sa" value = "Search"/>
</Div>
</Form>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Input [name = 'q']"). focus ();
}) </Script>
</Body>
</Html>


Jquery extension text box focus Method

In different browsers, if you only set focus () for a text box, the cursor focus may be at the beginning. The following code expands the textFocus Method for jquery to focus on the text box and enables the cursor to end with $ ("input"). textFocus (). You can also input a numeric parameter to set the cursor focus position. For example, $ ("input"). textFocus (2), the cursor is behind the second character.
Copy codeThe 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 (); // create a range in the text box
V = 0? Range. collapse (false): range. move ("character", v); // range collapse
Range. select (); // select
} Else {
Len = this. value. length;
V = 0? This. setSelectionRange (len, len): this. setSelectionRange (v, v); // dom directly sets the selection, and then focus
}
This. focus ();
});
Return this;
}
}) (JQuery)

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.