基于JavaScript实现表格隔行换色

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

基于JavaScript实现表格隔⾏换⾊
表格隔⾏换⾊
需求分析
我们商品分类的信息太多,如果每⼀⾏都显⽰同⼀个颜⾊的话会让⼈看的眼花,为了提⾼⽤户体验,减少⽤户看错的情况,需要对表格进⾏隔⾏换⾊
技术分析
table对象集合 cells[]:返回包含表格中所有单元格的⼀个数组。

rows[]:返回包含表格中所有⾏的⼀个数组。

tBodies[]:返回包含表格中所有tbody 的⼀个数组。

步骤分析
确定事件: ⽂档加载完成 onload 2. 事件要触发函数: init() 3. 函数:操作页⾯的元素要操作表格中每⼀⾏动态的修改⾏的背景颜⾊
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script >
function init(){
//得到表格
var tab = document.getElementById("tab");
//得到表格中每⼀⾏
var rows = tab.rows;
//便利所有的⾏,然后根据奇数偶数
for(var i=1; i < rows.length; i++){
var row = rows[i]; //得到其中的某⼀⾏
if(i%2==0){
row.bgColor = "yellow";
}else{
row.bgColor = "red"
}
}
}
</script>
</head>
<body onload="init()">
<table border="1px" width="600px" id="tab">
<tr>
<td>
<input type="checkbox"/>
</td>
<td>分类ID</td>
<td>分类名称</td>
<td>分类商品</td>
<td>分类描述</td>
<td>操作</td>
</tr>
<tr>
<td>
<input type="checkbox"/>
</td>
<td>1</td>
<td>⼿机数码</td>
<td>华为,⼩⽶,尼康</td>
<td>⿊马数码产品质量最好</td>
<td>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a>|<a </td>
</tr>
<tr>
<td>
<input type="checkbox"/>
</td>
<td>2</td>
<td>成⼈⽤品</td>
<td>充⽓的</td>
<td>这⾥⾯的充⽓电动硅胶的</td>
<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> </tr>
<tr>
<td>
<input type="checkbox"/>
</td>
<td>3</td>
<td>电脑办公</td>
<td>联想,⼩⽶</td>
<td>笔记本特卖</td>
<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> </tr>
<tr>
<td>
<input type="checkbox"/>
</td>
<td>4</td>
<td>馋嘴零⾷</td>
<td>辣条,⿇花,黄⽠</td>
<td>年货</td>
<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> </tr>
<tr>
<td>
<input type="checkbox"/>
</td>
<td>5</td>
<td>床上⽤品</td>
<td>床单,被套,四件套</td>
<td>都是套⼦</td>
<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> </tr>
</table>
</body>
</html>
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

相关文档
最新文档