没有 JavaScript 的默认 html 表单焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4166423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:14:52  来源:igfitidea点击:

Default html form focus without JavaScript

htmlaccessibility

提问by Adam Jenkin

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

是否可以在不使用 JavaScript 的情况下在 HTML 表单上设置默认输入焦点,例如:

<html>
  <form>
    Input 1: <input type="text" name="textbox1"/>
    <br/>
    Input 2: <input type="text" name="textbox2"/>
  </form>
</html>

I want to set the default focus to either of the text-boxes when the form loads without using JavaScript (as I want the behaviour to occur when a user has js disabled).

我想在不使用 JavaScript 的情况下加载表单时将默认焦点设置为任一文本框(因为我希望在用户禁用 js 时发生这种行为)。

回答by Jake Lucas

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

您可以在 HTML5 中执行此操作,但除此之外,您必须使用 JavaScript。

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

HTML5 允许您添加autofocus到表单元素,例如:

<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.

这在支持 HTML5 的浏览器(或者更确切地说,支持 HTML5 的这一特定部分的浏览器)中确实有效,但如您所知,并不是每个人都可以使用它。

回答by Greg

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.

需要注意的事情...如果您设置了焦点表单元素,那么任何使用辅助技术 (AT) 的人(如屏幕阅读器)都需要备份以查看菜单和焦点字段之前的任何其他内容。

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.

在我看来,首选方法是不要将焦点设置到任何字段,除非可用的跳过链接。这使他们可以选择跳入页面内容或从上到下阅读页面。

回答by Greg

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 accesskeyattribute. 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...

正如其他人所说,没有 Javascript 你不能保证默认字段。如果您有多个用户可能想要访问的字段,则您可能想要尝试的另一种选择是使用该accesskey属性。这基本上意味着用户可以在浏览过程中立即返回到任一字段,这对于屏幕阅读器等用户来说可能会派上用场......

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

关于这个主题的维基百科文章非常有用 - http://en.wikipedia.org/wiki/Access_key

回答by Evan Mulawski

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

如果没有某种形式的脚本,这是不可能的。甚至 Google 的主页也需要 Javascript 来聚焦搜索领域。

回答by Cahit

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

不过,您也许可以使用tabindex属性并使用默认文本框上的最低值。在此处查看浏览器支持:

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

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)

(在几乎所有其他情况下——即表单控件和链接——tabindex 有很好的支持)