当网页加载时,如何自动将焦点设置为文本框?

Mar*_*iek 158 html javascript

当网页加载时,如何自动将焦点设置为文本框?

是否有HTML标签可以执行此操作,还是必须通过Javascript完成?

Ben*_*man 240

如果你正在使用jquery:

$(function() {
  $("#Box1").focus();
});
Run Code Online (Sandbox Code Playgroud)

或原型:

Event.observe(window, 'load', function() {
  $("Box1").focus();
});
Run Code Online (Sandbox Code Playgroud)

或普通的javascript:

window.onload = function() {
  document.getElementById("Box1").focus();
};
Run Code Online (Sandbox Code Playgroud)

虽然请记住,这将替换其他负载处理程序,因此在谷歌中查找addLoadEvent()以便安全地附加onload处理程序而不是替换.

  • 因为将javascript与HTML分开会更加清晰.您应该将脚本行为添加到HTML中的可视元素. (7认同)
  • 为什么不把处理程序放在body元素上?任何参考?谢谢 (3认同)
  • @BenScheirman和5年后我们有JSX (3认同)

dav*_*010 91

在HTML中,所有表单字段都有一个autofocus属性.在Dive Into HTML 5中有一个很好的教程.不幸的是,目前不支持低于10的IE版本.

要使用HTML 5属性并回退到JS选项:

<input id="my-input" autofocus="autofocus" />
<script>
  if (!("autofocus" in document.createElement("input"))) {
    document.getElementById("my-input").focus();
  }
</script>
Run Code Online (Sandbox Code Playgroud)

不需要jQuery,onload或事件处理程序,因为JS位于HTML元素之下.

编辑:另一个优点是它可以在某些浏览器中使用JavaScript,并且当您不想支持旧浏览器时可以删除JavaScript.

编辑2:Firefox 4现在支持该autofocus属性,只是在没有支持的情况下离开IE.

  • 回到XHTML和HTML4时代,对于布尔属性,通常会使用`attribute ="value"`.HTML5带有"空属性语法",我们删除了冗余.现在我们可以做`<输入已检查的已禁用自动对焦数据 - >> (6认同)
  • 什么是`autofocus ="aufofocus"`?您提供的所有链接只是说添加属性:`autofocus`. (3认同)

Esp*_*spo 15

你需要使用javascript:

<BODY onLoad="document.getElementById('myButton').focus();">
Run Code Online (Sandbox Code Playgroud)

@Ben注意到你不应该像这样添加事件处理程序.虽然这是另一个问题,但他建议您使用此功能:

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在您的页面上调用addLoadEvent并引用一个函数,将焦点设置为您想要的文本框.


小智 12

只需在文本字段中编写自动对焦即可.这很简单,它的工作原理如下:

 <input name="abc" autofocus></input>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


Jon*_*Jon 10

使用普通的香草HTML和JavaScript

<input type='text' id='txtMyInputBox' />


<script language='javascript' type='text/javascript'>
function SetFocus()
{
    // safety check, make sure its a post 1999 browser
    if (!document.getElementById)
    {
        return;
    }

    var txtMyInputBoxElement = document.getElementById("txtMyInputBox");

    if (txtMyInputBoxElement != null)
    {
        txtMyInputBoxElement.focus();
    }
}
SetFocus();
</script>
Run Code Online (Sandbox Code Playgroud)

对于那些使用.net框架和asp.net 2.0或更高版本的人来说,它是微不足道的.如果您使用的是旧版本的框架,则需要编写一些类似于上面的javascript.

在OnLoad处理程序中(如果使用visual studio提供的库存页面模板,通常为page_load),您可以使用:

C#

protected void PageLoad(object sender, EventArgs e)
{
    Page.SetFocus(txtMyInputBox);
}
Run Code Online (Sandbox Code Playgroud)

VB.NET

Protected Sub PageLoad(sender as Object, e as EventArgs)

    Page.SetFocus(txtMyInputBox)

End Sub
Run Code Online (Sandbox Code Playgroud)

(*注意我从函数名称中删除了下划线字符,通常是Page_Load,因为在代码块中它拒绝正确呈现!我在标记文档中看不到如何使下划线呈现未转义.)

希望这可以帮助.


Ami*_*ahr 10

您可以通过以下方式使用jquery轻松完成:

<script type="text/javascript">

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

</script>
Run Code Online (Sandbox Code Playgroud)

通过调用此函数$(document).ready().

这意味着当DOM准备就绪时,该函数将执行.

有关READY功能的更多信息,请访问:http://api.jquery.com/ready/


rev*_*ive 7

恕我直言,选择页面上第一个可见的启用文本字段的"最干净"的方法是使用jQuery并执行以下操作:

$(document).ready(function() {
  $('input:text[value=""]:visible:enabled:first').focus();
});
Run Code Online (Sandbox Code Playgroud)

希望有帮助......

谢谢...


Joe*_*orn 6

<html>  
<head>  
<script language="javascript" type="text/javascript">  
function SetFocus(InputID)  
{  
   document.getElementById(InputID).focus();  
}  
</script>  
</head>  
<body onload="SetFocus('Box2')">  
<input id="Box1" size="30" /><br/>  
<input id="Box2" size="30" />  
</body>  
</html>  
Run Code Online (Sandbox Code Playgroud)

  • 太多了```你需要:`<body onload ="SetFocus('Box2')">` (2认同)

Vin*_*ert 5

作为一般建议,我建议不要从地址栏中窃取焦点.(杰夫已经谈过这个.)

网页可能需要一些时间才能加载,这意味着您的焦点更改可能会在用户键入pae URL后很长时间内发生.然后他可以改变主意并回到网址输入,同时你将加载页面并窃取焦点将其放入文本框中.

这是让我删除Google作为我的起始页面的唯一原因.

当然,如果您控制网络(本地网络)或焦点更改是为了解决重要的可用性问题,请忘记我刚才说的所有:)