简体   繁体   中英

Select text box on page load jQuery

I have the following code which uses JavaScript to select a text box when the page loads. I have included a copy of my code below. My question is how can this be done in jQuery?

<!doctype html>
<html>
<head>

</head>
<body>

<input id="query">

   <script type="text/javascript">
      function box(query){
         document.getElementById('query').focus();
      }
      box('query');
   </script>

</body>
</html>

Thanks in advance, Callum

You also can use HTML5:

<input type="text" autofocus>

Reference: http://www.w3schools.com/html5/att_button_autofocus.asp

Cross Browser Compatibility: http://caniuse.com/#feat=autofocus

$(document).ready(function(){
     $('#query').focus()
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM