最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select text box on page load jQuery - Stack Overflow

programmeradmin6浏览0评论

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

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

Share Improve this question asked May 13, 2011 at 17:24 Callum WhyteCallum Whyte 2,42911 gold badges38 silver badges56 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

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()
});
发布评论

评论列表(0)

  1. 暂无评论