文本框 - >立即开始输入,而不是单击该框

时间:2022-11-19 21:57:30

I have a search bar, and when I open the search bar I want to start typing right away and do not want to click on the text box for that to happen.

我有一个搜索栏,当我打开搜索栏时,我想立即开始输入,并且不想点击文本框来实现。

I tried to use myTextBoxId.click() ( because when you click it, you can start typing ) but it didn't work.

我试图使用myTextBoxId.click()(因为当你点击它时,你可以开始输入),但它没有用。

6 个解决方案

#1


3  

You have to

你必须

document.getElementById(" <put your Id here> ").focus();

to set focus on the given element.

将重点放在给定元素上。

#2


2  

You can set the focus once you load the page, or trigger the focus on keydown (this way even if you set the focus out of the input - when you start typing the focus will get there again).

您可以在加载页面后设置焦点,或者在keydown上触发焦点(这样即使您将焦点设置在输入之外 - 当您开始键入时,焦点将再次到达那里)。

$(function() {
  $('#searchbox').focus();

  $(document).on('keydown', function() {
    $('#searchbox').focus();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="searchbox" />

#3


2  

since you've got 17 of the same answer, how about a new one!

因为你有17个相同的答案,一个新的怎么样!

HTML5 has an autofocus attribute that does this. no JS required.

HTML5具有自动聚焦属性,可以执行此操作。不需要JS。

<input type='text' autofocus>

See here.

看这里。

#4


1  

Use .focus() instead of .click()

使用.focus()代替.click()

var myTextBoxId = document.getElementById("myTextBoxId")
myTextBoxId.focus();
<input id="myTextBoxId" />

#5


1  

If <input id="myTextBoxId" />, you can use following scripts

如果,则可以使用以下脚本

Jquery:

jQuery的:

$(function() {
 $("#myTextBoxId").focus();
});

Javascript:

使用Javascript:

window.onload = function() {
 document.getElementById("myTextBoxId").focus();
};

or

要么

<body onLoad="document.getElementById('myTextBoxId').focus();">

#6


1  

You can use myTextBox.focus() to achieve this. Note that you can call focus() after myTextBox variable is set to point to your actual text box. See: HTMLElement focus

您可以使用myTextBox.focus()来实现此目的。请注意,在myTextBox变量设置为指向实际文本框后,可以调用focus()。请参阅:HTMLElement焦点

#1


3  

You have to

你必须

document.getElementById(" <put your Id here> ").focus();

to set focus on the given element.

将重点放在给定元素上。

#2


2  

You can set the focus once you load the page, or trigger the focus on keydown (this way even if you set the focus out of the input - when you start typing the focus will get there again).

您可以在加载页面后设置焦点,或者在keydown上触发焦点(这样即使您将焦点设置在输入之外 - 当您开始键入时,焦点将再次到达那里)。

$(function() {
  $('#searchbox').focus();

  $(document).on('keydown', function() {
    $('#searchbox').focus();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="searchbox" />

#3


2  

since you've got 17 of the same answer, how about a new one!

因为你有17个相同的答案,一个新的怎么样!

HTML5 has an autofocus attribute that does this. no JS required.

HTML5具有自动聚焦属性,可以执行此操作。不需要JS。

<input type='text' autofocus>

See here.

看这里。

#4


1  

Use .focus() instead of .click()

使用.focus()代替.click()

var myTextBoxId = document.getElementById("myTextBoxId")
myTextBoxId.focus();
<input id="myTextBoxId" />

#5


1  

If <input id="myTextBoxId" />, you can use following scripts

如果,则可以使用以下脚本

Jquery:

jQuery的:

$(function() {
 $("#myTextBoxId").focus();
});

Javascript:

使用Javascript:

window.onload = function() {
 document.getElementById("myTextBoxId").focus();
};

or

要么

<body onLoad="document.getElementById('myTextBoxId').focus();">

#6


1  

You can use myTextBox.focus() to achieve this. Note that you can call focus() after myTextBox variable is set to point to your actual text box. See: HTMLElement focus

您可以使用myTextBox.focus()来实现此目的。请注意,在myTextBox变量设置为指向实际文本框后,可以调用focus()。请参阅:HTMLElement焦点