最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

利用js实现表格隔行换色效果

来源:懂视网 责编:小采 时间:2020-11-03 12:30:55
文档

利用js实现表格隔行换色效果

利用js实现表格隔行换色效果:首先我们来看一下实现的效果,如下图所示:思路:确定事件: 文档加载完成 onload 2. 事件要触发函数: init() 3. 函数:操作页面的元素 要操作表格中每一行 动态的修改行的背景颜色具体代码:<!DOCTYPE html> <html> <he
推荐度:
导读利用js实现表格隔行换色效果:首先我们来看一下实现的效果,如下图所示:思路:确定事件: 文档加载完成 onload 2. 事件要触发函数: init() 3. 函数:操作页面的元素 要操作表格中每一行 动态的修改行的背景颜色具体代码:<!DOCTYPE html> <html> <he

首先我们来看一下实现的效果,如下图所示:

5482894161c362bdf2bed77cec89a47.png

思路:

确定事件: 文档加载完成 onload 2. 事件要触发函数: init() 3. 函数:操作页面的元素 要操作表格中每一行 动态的修改行的背景颜色

具体代码:

<!DOCTYPE html>
<html>
<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 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>
 </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>|<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></td>
 </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>|<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></td>
 </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>|<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></td>
 </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>|<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></td>
 </tr>
</table>
</body>
</html>

推荐教程:js入门教程

文档

利用js实现表格隔行换色效果

利用js实现表格隔行换色效果:首先我们来看一下实现的效果,如下图所示:思路:确定事件: 文档加载完成 onload 2. 事件要触发函数: init() 3. 函数:操作页面的元素 要操作表格中每一行 动态的修改行的背景颜色具体代码:<!DOCTYPE html> <html> <he
推荐度:
标签: 表格 实现 js
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top