网页加载时如何自动将焦点设置为文本框? [英] How do you automatically set the focus to a textbox when a web page loads?

查看:137
本文介绍了网页加载时如何自动将焦点设置为文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Web页面加载时自动将焦点设置到文本框?



是否有一个HTML标记可以执行此操作,还是必须通过Javascript?

解决方案

任何javascript书籍都会告诉您不要将处理程序放在body元素上。



如果您使用的是jquery:

  $(function(){
$(#Box1)。focus();
});

或原型:

  Event.observe(window,'load',function(){
$(Box1)。focus();
});

或简单的javascript:

  window.onload = function(){
document.getElementById(Box1)。focus();
};虽然请记住,这将取代其他负载处理程序,所以请在google中查找addLoadEvent()。为了安全的方式来追加onload处理程序而不是替换。


How do you automatically set the focus to a textbox when a web page loads?

Is there an HTML tag to do it or does it have to be done via Javascript?

解决方案

Any javascript book will tell you not to put handlers on the body element like that.

I'f you're using jquery:

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

or prototype:

Event.observe(window, 'load', function() {
  $("Box1").focus();
});

or plain javascript:

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

though keep in mind that this will replace other on load handlers, so look up addLoadEvent() in google for a safe way to append onload handlers rather than replacing.

这篇关于网页加载时如何自动将焦点设置为文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆