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处理程序而不是替换.
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.
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
只需在文本字段中编写自动对焦即可.这很简单,它的工作原理如下:
Run Code Online (Sandbox Code Playgroud)<input name="abc" autofocus></input>
希望这可以帮助.
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/
恕我直言,选择页面上第一个可见的启用文本字段的"最干净"的方法是使用jQuery并执行以下操作:
$(document).ready(function() {
$('input:text[value=""]:visible:enabled:first').focus();
});
Run Code Online (Sandbox Code Playgroud)
希望有帮助......
谢谢...
<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)