使用 javascript 在输入字段外输入时聚焦并输入输入字段

focus and type into input field when typing outside the input field with javascript

如标题所述,我想在输入字段外输入时输入输入字段。
使用当前代码,当我在外面打字时,我可以将注意力集中在输入字段上,但似乎无法让我按下的键出现在上面。请帮助..

喜欢 google.com 打字时没有聚焦输入字段。

<form id="searchForm" name="search">
<input id="searchBox" name="search_text">
</form>

<script type="text/javascript">

        $("body").on("keyup", function(e) {
            $("#searchForm[name='search_text']").focus();
        });
</script>

试试这个例子:

$(function() {
  $(document).on("keydown", function(e) {
    e.stopPropagation();
    $("#searchBox").focus();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="searchForm" name="search">
  <input id="searchBox" name="search_text">
</form>

试试这个:

<style>
body, div, input {
   color: #afa;
   text-shadow: 0px 0px 1px #fff;
}
<style>
<input id="searchBox" name="search_text" style="color: transparent">
<script type="text/javascript">
    $("body").on("keyup", function(e) {
        $("#searchBox").focus();
    });
 </script>

你可以只用Javascript(不需要jQuery):

function inputFocus(){
   document.getElementById("searchBox").focus();
}
   window.onkeydown = inputFocus;

the working DEMO