Default html form focus without JavaScript

后端 未结 5 2144
傲寒
傲寒 2020-12-01 03:48

Is it possible to set the default input focus on an HTML form without using JavaScript, for example:


  
Input 1:
相关标签:
5条回答
  • 2020-12-01 04:30

    You can do it in HTML5, but otherwise, you must use JavaScript.

    HTML5 allows you to add autofocus to your form element, eg:

    <input type="text" name="myInput" autofocus />
    

    This does work in browsers which support HTML5 (Or rather, browsers which support this particular part of HTML5) but as you know, not everybody can use it yet.

    0 讨论(0)
  • 2020-12-01 04:33

    As others have said, without Javascript you can't guarantee a default field. An alternative option you might want to try, if you have multiple fields that a user might want to access is using the accesskey attribute. This will essentially mean a user can return to either of the fields instantly later during browsing, which may come in handy for users of screen readers, etc...

    Wikipedias article on this subject is quite useful - http://en.wikipedia.org/wiki/Access_key

    0 讨论(0)
  • 2020-12-01 04:45

    Something to be aware of ... if you set a focused form element, then anyone using Assisted Technology (AT) like a screen reader will need to back up to see menus and any other content that is before the focused field.

    A preferred method, in my opinion , is to not set focus to any field, except a skip-link if its available. That gives them the option to skip into the pages content or read the page from the top down.

    0 讨论(0)
  • 2020-12-01 04:46

    This is not possible without some form of scripting. Even Google's home page requires Javascript to focus the search field.

    0 讨论(0)
  • 2020-12-01 04:48

    You might be able to use the tabindex attribute and use the lowest value on the default textbox though. Check here for browser support:

    http://reference.sitepoint.com/html/object/tabindex#compatibilitysection

    The site suggests that

    (in almost all other cases—namely form controls and links—the tabindex has excellent support)

    0 讨论(0)
提交回复
热议问题