下面的实例是建立一个表格,对class进行动态操作效果如图所示:
<!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 src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#table tr").mouseover(function(){
$(this).addClass("over");
});
$("#table tr").mouseout(function(){
$(this).removeClass("over");
});
$("#table tr:even").addClass("double");
});
function removeEvenTdClass()
{
$("#table tr:even").removeClass();
}
</script>
<style type="text/css">
th{
background: gray;
color: white;
}
table{
width: 30em;
height: 10em;
}
td {
border-bottom:1px solid #95bce2;
text-align:center;
}
tr.over td{
font-weight: bold;
}
tr.double td{
background: #DAF3F1;
}
</style>
</head>
<body>
<table id="table" border="0" cellpadding="0" cellspacing="0">
<tr>
<th>姓名</th>
<th>年龄</th>
<th>籍贯</th>
<th>手机</th>
</tr>
<tr>
<td>nlcold</td>
<td>33</td>
<td>湖南</td>
<td>15988888888</td>
</tr>
<tr>
<td>nlcold</td>
<td>33</td>
<td>湖南</td>
<td>15988888888</td>
</tr>
<tr>
<td>nlcold</td>
<td>33</td>
<td>湖南</td>
<td>15988888888</td>
</tr>
<tr>
<td>nlcold</td>
<td>33</td>
<td>湖南</td>
<td>15988888888</td>
</tr>
<tr>
<td>nlcold</td>
<td>33</td>
<td>湖南</td>
<td>15988888888</td>
</tr>
<tr>
<td>nlcold</td>
<td>33</td>
<td>湖南</td>
<td>15988888888</td>
</tr>
<tr>
<td>nlcold</td>
<td>33</td>
<td>湖南</td>
<td>15988888888</td>
</tr>
<tr>
<td>nlcold</td>
<td>33</td>
<td>湖南</td>
<td>15988888888</td>
</tr>
<tr>
<td>nlcold</td>
<td>33</td>
<td>湖南</td>
<td>15988888888</td>
</tr>
</table>
<input id="btnRemoveClass" name="btnRemoveClass" type="button" value="removeEvenTdClass" οnclick="removeEvenTdClass()"/>
</body>
</html>