先看下面的例子代码:
<%@ 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