Javascript 如何在 BootStrap 中将焦点设置在输入的第一个字段上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14379536/
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-24 16:34:55  来源:igfitidea点击:

How to set focus on first field of input in BootStrap?

javascripthtmltwitter-bootstrap

提问by Dharmik Bhandari

Possible Duplicate:
How to set the focus to the first input element in an HTML form independent from the id?

可能的重复:
如何将焦点设置为独立于 id 的 HTML 表单中的第一个输入元素?

I'm using BootStrap with ASP.NET MVC4. There is one login form but I'm unable to set focus on first input field.

我在 ASP.NET MVC4 中使用 BootStrap。有一个登录表单,但我无法将焦点设置在第一个输入字段上。

How to set focus on first field of input? Is there any BootStrap class that uses to set focus?

如何将焦点设置在输入的第一个字段上?是否有任何用于设置焦点的 BootStrap 类?

回答by Jeromy French

There isn't a Bootstrap-specific class or function that does this*, but HTML5 provides the autofocusattributefor input fields.

没有特定于 Bootstrap 的类或函数可以执行此操作*,但 HTML5为输入字段提供了autofocus属性

This attribute currently enjoys wide browser support.

此属性目前享有广泛的浏览器支持

*-Since you are using Bootsrap, and Bootstrap is powered by jQuery, you could "shim" older browsers that otherwise don't support autofocus:

*-由于您使用的是 Bootsrap,并且 Bootstrap 由 jQuery 提供支持,因此您可以“填充”不支持自动对焦的旧浏览器:

$('[autofocus]:first').focus();