打开网页时文本框自动获得焦点 .

字体大小: 中小 标准 ->行高大小: 标准
方法有很多种,这里介绍两种:

 

1. 格式<body onload=document.formname.textname.focus()>  

    formname为表单名称,后面接着的textname 是文本框的名称。

    示例: 

    <html> 
    <head></head> 
    <body onLoad="document.user.username.focus()" > 
    <form name="user" action="">
    <input type="text" name="username"> 
    </form>
    </body> 
    </html>  

    这个方法比较简单,推荐使用。

2.  <html> 
    <head></head> 
    <body > 
    <input type="text" id="name"> <!--设置文本输入框的id为name--> 
    <script language="javascript"> 
    document.getElementById('name').focus();  
    <!--设置id为name的元素得到焦点--> 
    </script> 
    </body> 
    </html>  

   这种方法使用了JavaScript,相对麻烦一些,但它的功能很强大,比较灵活。

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/67595.html