简体   繁体   中英

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. 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. What code can I add to make sure the text input box is immediately typeable?

Use autofocus :

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

From the input documentation on 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. It cannot be applied if the type attribute is set to hidden (that is, you cannot automatically set focus to a hidden control). Note that the focusing of the control may occur before the firing of the DOMContentLoaded event.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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