简体   繁体   English

jQuery:选择一个文本输入框

[英]jQuery: Make a text input box selected

You know how when you open a new tab, you can start typing without having to select the search bar? 您知道打开新标签页时如何开始输入而不必选择搜索栏吗? I've got a text input box in HTML, and I'd like to be able to open my webpage and have that text input box immediately typeable, for lack of a better term. 我有一个HTML文本输入框,并且我想能够打开我的网页并立即输入该文本输入框,以免缺少更好的用语。 Say my input box looks like this: 说我的输入框看起来像这样:

 <input type="text" class="myInput" value="add an item"></input>

I'm using HTML, CSS and JavaScript/jQuery right now. 我现在正在使用HTML,CSS和JavaScript / jQuery。 What code can I add to make sure the text input box is immediately typeable? 我可以添加什么代码来确保文本输入框可立即输入?

Use autofocus : 使用autofocus

<input type="text" class="myInput" value="add an item" autofocus/>

From the input documentation on MDN : MDN的输入文档中

This Boolean attribute lets you specify that a form control should have input focus when the page loads, unless the user overrides it (eg by typing in a different control). 使用此布尔属性,可以指定页面加载时表单控件应具有输入焦点,除非用户将其覆盖(例如,通过键入其他控件)。 Only one form element in a document can have the autofocus attribute, which is a Boolean. 文档中只有一个表单元素可以具有autofocus属性,该属性是布尔值。 It cannot be applied if the type attribute is set to hidden (that is, you cannot automatically set focus to a hidden control). 如果将type属性设置为hidden(即,您不能将焦点自动设置为hidden控件),则不能应用它。 Note that the focusing of the control may occur before the firing of the DOMContentLoaded event. 请注意,控件的焦点可能发生在DOMContentLoaded事件的触发之前。

<input type="text" class="myInput" value="add an item" autofocus>

https://www.w3schools.com/TAgs/att_input_autofocus.asp

在html中使用自动对焦,在jQuery中使用$('。myInput')。focus()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM