点击表格中的复选框使其背景变色
使用css代码写出页面的布局
<style>
table,table td{
border: 1px solid blueviolet;
border-collapse: collapse;
}
table,table th{
border: 1px solid blueviolet;
border-collapse: collapse;
margin: auto;
}
td{
width: 80px;
height: 30px;
text-align: center;
}
.odd{
background-color: #CCCCCC;
}
.even{
background-color: bisque;
}
.checked{
background-color: yellow;
}
</style>
html代码写出表格
<table>
<tr>
<th></th>
<th>姓名</th>
<th>薪水</th>
<th>年龄</th>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>张三</td>
<td>2000</td>
<td>18</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>李四</td>
<td>20000</td>
<td>38</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>王麻子</td>
<td>12000</td>
<td>25</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>马六</td>
<td>2500</td>
<td>20</td>
</tr>
</table>
最后使用jQuery写出点击使背景颜色变色的效果 使用toggleClass() 对设置或移除被选元素的一个或多个类进行切换。
<script>
$(function(){
$("tr:odd").addClass("odd");
$("tr:even").addClass("even");
$(":checkbox").on("click",function(){
$(this).parent().parent().toggleClass("checked");
});
});
</script>
```**效果**
