JavaScript实例---表格隔行变色以及移入鼠标高亮

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>index</title>
<style type="text/css">
	#div1{width: 100px;height:100px;background: red;}
</style>
<script type="text/javascript">
window.onload = function()
{
	var oTab = document.getElementById('tab1');
	var oldColor = '';
	for(var i=0;i<oTab.tBodies[0].rows.length;i++)
	{
		oTab.tBodies[0].rows[i].onmouseover = function()
		{
			oldColor = this.style.background;
			this.style.background = "green";
		}
		oTab.tBodies[0].rows[i].onmouseout = function()
		{
			this.style.background = oldColor;
		}
		if(i%2)
		{
			oTab.tBodies[0].rows[i].style.background = 'red';
		}
		else
		{
			oTab.tBodies[0].rows[i].style.background= " ";
		}
	}
}

</script>
</head>
<body>
	<table border="1" width="300" id="tab1">
		<thead>
			<td>ID</td>
			<td>NAME</td>
			<td>AGE</td>
		</thead>
		<tbody> 
			<tr>
				<td>1</td>
				<td>John</td>
				<td>27</td>
			</tr>
			<tr>
				<td>2</td>
				<td>Lucy</td>
				<td>27</td>
			</tr>
			<tr>
				<td>3</td>
				<td>Dan</td>
				<td>26</td>
			</tr>
			<tr>
				<td>4</td>
				<td>Ben</td>
				<td>30</td>
			</tr>
		</tbody>
	</table>
</body>
</html>

  

posted @ 2015-05-16 10:02  calmound  阅读(270)  评论(0编辑  收藏  举报