第六章第二堂课后续

表单验证

表单是HTML中非常重要的部分,几乎每个网页上都有表单,例如,用户提交信息,查询信息。在表单
中,表单验证也是非常重要的。
用blur事件判断用户名和密码不能为空,并且密码不能小于8位。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>表单验证</title>
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function(){
	$("form :input.required").each(function(){                  
		var $required = $("<strong class='star'>*</strong>");     
		$(this).parent().append($required);                       
	})
	$("form :input").blur(function(){
		if($(this).is("#username")){        
			if($(this).val() == ""){        
				alert("用户名不能为空!");
			}
		}
		if($(this).is("#pwd")){            
			if($(this).val() == ""){        
				alert("密码不能为空!");
			}
			if(this.value.length < 8){      
				alert("密码不能小于8位,请重新输入!");
			}
		}
	})
})
</script>
</head>

<body>
	<form>
		<h3 align="center">用户注册</h3>
		<div class="dt">用户名:<input type="text" id="username" name="username" size=20 class="required" /></div>
		<div class="dt">密&nbsp;&nbsp;码:<input type="password" id="pwd" name="pwd" size=20 class="required" /></div>
		<div class="dt">性&nbsp;&nbsp;别:<input  type="text" id="sex" name="sex" size=4 maxlength=3 /></div>
		<div class="dt">年&nbsp;&nbsp;龄:<input type="text" id="age" name="age" size=4 maxlength=3 /></div>
		<div class="dt">
			<input type="submit" name="sub" value="注册" />
		</div>
	</form>
</body>
</html>

使用jQuery操作表格

表单换色

实现表格的的隔行换色,首先需要为表格的奇数行和偶数行设定样式,之后使用jQuery为表格的奇数行和
偶数行分别添加样式。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>表格隔行换色</title>
<style>
.a{
	background:lightblue;
}
.b{
	background:yellow;
}
</style>
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function(){
	$("tbody>tr:odd").addClass("a");   
	$("tbody>tr:even").addClass("b");   
})
</script>
</head>
<body>
<table border="1" align="center">
  <caption>JavaScript开发非常之旅套系图书</caption>
  <thead bgcolor="#B2B2B2" align="center" valign="bottom">
  	 <tr>
  	   <th>书名</th>
 	   <th>出版单位</th>
 	 </tr>
  </thead>
 <tbody>
  <tr>
    <td width="185">JavaScript从入门到精通</td>
    <td width="220">吉林省明日科技有限公司</td>
  </tr>
  <tr>
    <td>JavaScript自学视频教程</td>
    <td>吉林省明日科技有限公司</td>
  </tr>
  <tr>
    <td>JavaScript程序设计</td>
    <td>吉林省明日科技有限公司</td>
  </tr>
  <tr>
    <td>jQuery开发基础教程</td>
    <td>吉林省明日科技有限公司</td>
  </tr>
  <tr>
    <td>jQuery从入门到精通</td>
    <td>吉林省明日科技有限公司</td>
  </tr>
  </tbody>
</table>
</body>
</html>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值