隔行换色(修改版)

本文介绍如何使用CSS为HTML表格实现隔行换色的效果,通过选择奇偶行应用不同背景色,提升表格可读性。
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript" src="js/jquery-3.3.1.js" ></script>
		<style>
			table{
				border:1px solid #ccc;
				font-family:"微软雅黑";
			}
			.qblue{
				background-color: lightyellow;
			}
			.qpink{
				background-color: pink;
			}
			
			.float_color{
				background-color: mistyrose;
			}
			
		</style>
		
		<script>
			$(function(){
				//table表格内部有一个tbody,自动生成,会隐藏,存在
				//$("table > tbody > tr").addClass("qblue");
				//跨层级获取标签  直接加空格
				$("table tr:first").css("background-color","darkcyan");
				//隔行换色添加颜色
				$("table tr:even:gt(0)").addClass("qblue");
				
				$("table tr:odd").addClass("qpink");
				
				//添加小手
				$("table tr").css("cursor","pointer");
				// this:当前对象  绑定事件 dom对象:this  jquery对象:$(this)
				$("table tr:gt(0)").mouseover(function(){
					$(this).addClass("float_color");
					
				});
				//移出事件
				$("table tr:gt(0)").mouseout(function(){
					$(this).removeClass("float_color");
				});
				
				
				
				
			});
			
		</script>
		
	</head>
	<body>
		<center>
			<table border="0px" cellspacing="0" cellpadding="7px" width="500px">
					<!--4*2-->
					<!--tr*4>td*2   快捷键-->
					<tr>
						<th>名称</th>
						<th>爱好</th>
					</tr>
					<tr>
						<td align="center">李逵</td>
						<td>喝酒、赌博、吹牛</td>
					</tr>
					<tr>
						<td align="center">张飞</td>
						<td>喝酒、打骂军士、睁眼睡觉</td>
					</tr>
					<tr>
						<td align="center">孙悟空</td>
						<td>打妖怪、吃桃</td>
					</tr>	
					<tr>
						<td align="center">贾宝玉</td>
						<td>富贵闲人、喜欢各种小姐姐</td>
					</tr>
			</table>
		</center>
	</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值