html:form标签的focus属性的作用
费秦迟
2023-12-01
你可以用focus属性来生成JavaScript,它会“定焦”(focus)到该form所包含的一个元素上。使用focus属性时你需要给它指定元素的名称。比如,以下代码是定焦在第二个Text元素上的:
<body>
<html:form action="/login" focus="password">
User Name: <html:text property="userName"/>
<br>Password: <html:text property="password"/>
<br><html:submit/>
</html:form>
</body>
该段代码会被转换成:
<body>
<form name="loginForm" method="post" action="/myStrutsApp6/login.do">
User Name: <input type="text" name="userName" value="">
<br>Password: <input type="text" name="password" value="">
<br><input type="submit" value="Submit">
</form>
<script language="JavaScript" type="text/javascript">
<!--
if (document.forms["loginForm"].elements[
"password"].type != "hidden")
document.forms["loginForm"].elements[
"password"].focus()
// -->
</script>
</body>