javascript - jQuery:选中一个文本输入框

标签 javascript jquery html css

您知道如何在打开新标签页时无需选择搜索栏就可以开始输入吗?我有一个 HTML 文本输入框,我希望能够打开我的网页并立即输入该文本输入框,因为没有更好的术语。假设我的输入框是这样的:

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

我现在正在使用 HTML、CSS 和 JavaScript/jQuery。我可以添加什么代码来确保文本输入框可以立即输入?

最佳答案

使用自动对焦:

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

来自 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 (e.g. 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.

关于javascript - jQuery:选中一个文本输入框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46498545/

相关文章:

javascript - d3 : repeat transition for series of elements?

javascript - PHP Access-Control-Allow-Origin header 不起作用

jquery - 悬停时延迟显示内联 block

php - 将值从 php 数组传输到 jquery

javascript - 在 iOS Safari 中模拟/触发选择框(可能使用 touchend)

javascript - 通过 JQuery 和 JavaScript 函数提交按钮所做的更改不会持续

javascript - HTML - XML - 跳转到另一个选项卡/页面元素

html - 在 R igraph 中创建悬停按钮

javascript - jQuery UI Selectables - 从对象外部开始拖动选择

html - 如何使用 CSS 在 html 元素中的文本后插入内容?