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)