没有 JavaScript 的默认 html 表单焦点 [英] Default html form focus without JavaScript
问题描述
是否可以在不使用 JavaScript 的情况下在 HTML 表单上设置默认输入焦点,例如:
Is it possible to set the default input focus on an HTML form without using JavaScript, for example:
<html>
<form>
Input 1: <input type="text" name="textbox1"/>
<br/>
Input 2: <input type="text" name="textbox2"/>
</form>
</html>
我想在不使用 JavaScript 的情况下加载表单时将默认焦点设置为任一文本框(因为我希望在用户禁用 js 时发生这种行为).
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).
推荐答案
您可以在 HTML5 中完成,否则,您必须使用 JavaScript.
You can do it in HTML5, but otherwise, you must use JavaScript.
HTML5 允许您将 autofocus
添加到表单元素,例如:
HTML5 allows you to add autofocus
to your form element, eg:
<input type="text" name="myInput" autofocus />
这在支持 HTML5 的浏览器(或者更确切地说,支持 HTML5 的这一特定部分的浏览器)中确实有效,但如您所知,并不是每个人都可以使用它.
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.
这篇关于没有 JavaScript 的默认 html 表单焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!