jsp中光标焦点设置!

先看下面的例子代码:

<%@ page contentType="text/html; charset=gb2312"%>
<%session.invalidate();%>
<html>
 <head>
  <title>Untitled Document</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<SCRIPT language=javascript>
<!-- Begin--
function page()
{
  checkForm.userId.focus();
}

// End -->
</SCRIPT>
 </head>
 <LINK href="hellking2.css" type=text/css rel=stylesheet>

 <body>
  <center>
   <hr>
   ::请输入一个用户标识后点击登录::
   <br>
   ::只有用户名为"hellking"才能登录::
   <form action="checklogin.jsp" name="checkForm" method=get>
    <table width="30%" border="1">
     <tr bgcolor="#336600">
      <td>
       用户登录
      </td>
     </tr>
     <tr align="center" bgcolor="#CCCCCC">
      <td>
       用户名:
       <input type="text" name="userId">
      </td>
     </tr>
     <tr align="center" bgcolor="#CCCCCC">
      <td>
       用户名:
       <input type="password" name="password">
      </td>
     </tr>
     <tr align="center" bgcolor="#993399">
      <td align="center">
       <input type="submit" value="登录">
      </td>
     </tr>
    </table>
   </form>
  </center>
 </body>
</html>

这部分是一个登陆的表单,我想让每次打开这个页面的时候光标都停留在用户名的输入框里,怎么做呢?

 1.加入script代码

<SCRIPT language=javascript>
<!-- Begin--
function setFocus()
{
  checkForm.userId.focus();
}

// End -->
</SCRIPT>
2.在body属性里面设置onload属性

<body οnlοad=setFocus()>

这样就OK了。

完整的代码如下:

<%@ page contentType="text/html; charset=gb2312"%>
<%session.invalidate();%>
<html>
 <head>
  <title>Untitled Document</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<SCRIPT language=javascript>
<!-- Begin--
function setFocus()
{
  checkForm.userId.focus();
}

// End -->
</SCRIPT>
 </head>
 <LINK href="hellking2.css" type=text/css rel=stylesheet>

 <body οnlοad=setFocus()>
  <center>
   <hr>
   ::请输入一个用户标识后点击登录::
   <br>
   ::只有用户名为"hellking"才能登录::
   <form action="checklogin.jsp" name="checkForm" method=get>
    <table width="30%" border="1">
     <tr bgcolor="#336600">
      <td>
       用户登录
      </td>
     </tr>
     <tr align="center" bgcolor="#CCCCCC">
      <td>
       用户名:
       <input type="text" name="userId">
      </td>
     </tr>
     <tr align="center" bgcolor="#CCCCCC">
      <td>
       用户名:
       <input type="password" name="password">
      </td>
     </tr>
     <tr align="center" bgcolor="#993399">
      <td align="center">
       <input type="submit" value="登录">
      </td>
     </tr>
    </table>
   </form>
  </center>
 </body>
</html>


http://www.xiami.com/collect/210974967?success=3
http://www.xiami.com/collect/210974967?success=9
http://www.xiami.com/collect/210974967?spm=a1z1s.3329273.1561534893.2.t4um0zhttp://www.xiami.com/collect/209848293?success=2
http://www.xiami.com/collect/209848293?spm=a1z1s.3229273.1561534893.2.JxwhZn

http://www.xiami.com/collect/209848293?spm=a1z1s.6929273.1561534893.2.JxwhZn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值