我有一个网页表单的文本框。如何在默认情况下设置文本框的焦点?

就像这样:

<body onload='setFocusToTextBox()'>

有人能帮帮我吗?我不知道如何用JavaScript设置文本框的焦点。

<script>
  function setFocusToTextBox(){
    //What to do here
  }
</script>

这样做。

如果你的元素是这样的…

<input type="text" id="mytext"/>

你的剧本应该是

<script>
function setFocusToTextBox(){
    document.getElementById("mytext").focus();
}
</script>

对于简单的Javascript,试试下面的代码:

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

通常当我们关注文本框时,我们也应该滚动到视图

function setFocusToTextBox(){
    var textbox = document.getElementById("yourtextbox");
    textbox.focus();
    textbox.scrollIntoView();
}

检查一下是否有帮助。

不管怎样,你可以在HTML5兼容浏览器上使用自动聚焦属性。甚至可以在IE版本10上使用。

<input name="myinput" value="whatever" autofocus />

如前所述,文档。表单也有用。

function setFocusToTextBox( _element ) {
  document.forms[ 'myFormName' ].elements[ _element ].focus();
}

setFocusToTextBox( 0 );
// sets focus on first element of the form

如果你的代码是:

<input type="text" id="mytext"/>

如果你正在使用JQuery,你也可以使用这个:

<script>
function setFocusToTextBox(){
    $("#mytext").focus();
}
</script>

请记住,必须首先绘制输入$(document).ready()

我以前经常用这个:

<html>
  <head>
    <script type="text/javascript">
      function focusFieldOne() {
        document.FormName.FieldName.focus();
      }
    </script>
  </head>

  <body onLoad="focusFieldOne();">
    <form name="FormName">
      Field <input type="text" name="FieldName">
    </form>
  </body>
</html>

也就是说,你可以在HTML 5中使用autofocus属性。

请注意:我想更新这个旧线程,显示所问的示例,以及更新的,更容易的更新,供那些仍然在读这篇文章的人使用。;)

如果你的<input>或<textarea>有属性id=mytext,那么使用

mytext.focus();

函数setFocusToTextBox() { mytext.focus (); } <身体onload = ' setFocusToTextBox () > < >形式 <input type="text" id="mytext"/> > < /形式 < /身体>

窗口。Onload是最初放置焦点 Onblur是将焦点放在文本区域外,或避开文本区域 模糊

    <textarea id="focus"></textarea>
    <script>
     var mytexarea=document.getElementById("focus");
    window.onload=function()
    {
   
    mytexarea.focus();
    
    }
    

    </script>

试试这个:

$('.modal').on('shown.bs.modal', function () {
        setTimeout(function() {
                $("input#yourFieldId").addClass('modal-primary-focus').focus();
            },
            500);
    });

这个例子对我很有用

$(document).ready(function () {
document.getElementById('TextBox').focus();
}

Thought of sharing some edge cases for this subject. If your content is reloading (example dynamic DOM loading results from API and setting focus on first item of results) adding attribute autofocus will not be your solution, it works only on first load, second DOM change will not work but works fine in static DOM or single page load. If you have Dynamic component loading data simple .focus() will fail due to triggering focus on element not created yet by the time focus() is or blur not complete yet by DOM. For this case expected is to add delay time (setTimeout function) to give a time for focus to apply to new created or recreated element in DOM. My case was to load data from API and get focus on first result. Adding var el = document.getElementById(focusId); el.focus(); solved the issue so DOM completes blur without adding delay.

<input type="text" class="word"> //html code

let theinput = document.querySelector(".word"); //Get the input 
 theinput.focus(); // focus on input