js表格隔行变色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
table {
width: 300px;
text-align: center;
border-collapse: collapse;
}
td,
th {
border: 1px solid black;
}
</style>
</head>
<body>
<table id="tab">
<thead>
<th>编号</th>
<th>职务</th>
<th>操作</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Boos</td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>总经理</td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>副经理</td>
<td></td>
</tr>
<tr>
<td>4</td>
<td>组长</td>
<td></td>
</tr>
<tr>
<td>5</td>
<td>员工</td>
<td></td>
</tr>
<tr>
<td>6</td>
<td>员工</td>
<td></td>
</tr>
<tr>
<td>7</td>
<td>员工</td>
<td></td>
</tr>
<tr>
<td>8</td>
<td>员工</td>
<td></td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
<script>
var tab = document.getElementById("tab")
var rows = tab.tBodies[0].rows
for (var i = 0; i < rows.length; i++) {
rows[i].style['background-color'] = i % 2 == 0 ? 'green' : 'red'
}
</script>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a71449fd1cbd9324e30fd3e800953b1a.png#pic_center)