基于JavaScript實(shí)現(xiàn)表格隔行換色
表格隔行換色
需求分析
我們商品分類(lèi)的信息太多,如果每一行都顯示同一個(gè)顏色的話(huà)會(huì)讓人看的眼花,為了提高用戶(hù)體驗(yàn),減少用戶(hù)看錯(cuò)的情況,需要對(duì)表格進(jìn)行隔行換色
技術(shù)分析
table對(duì)象 集合 cells[]:返回包含表格中所有單元格的一個(gè)數(shù)組。 rows[]:返回包含表格中所有行的一個(gè)數(shù)組。 tBodies[]:返回包含表格中所有tbody 的一個(gè)數(shù)組。
步驟分析
確定事件: 文檔加載完成 onload 2. 事件要觸發(fā)函數(shù): init() 3. 函數(shù):操作頁(yè)面的元素 要操作表格中每一行 動(dòng)態(tài)的修改行的背景顏色
代碼實(shí)現(xiàn)
<!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;
//便利所有的行,然后根據(jù)奇數(shù) 偶數(shù)
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>分類(lèi)ID</td>
<td>分類(lèi)名稱(chēng)</td>
<td>分類(lèi)商品</td>
<td>分類(lèi)描述</td>
<td>操作</td>
</tr>
<tr>
<td>
<input type="checkbox"/>
</td>
<td>1</td>
<td>手機(jī)數(shù)碼</td>
<td>華為,小米,尼康</td>
<td>黑馬數(shù)碼產(chǎn)品質(zhì)量最好</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>這里面的充氣電動(dòng)硅膠的</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>聯(lián)想,小米</td>
<td>筆記本特賣(mài)</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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
小程序開(kāi)發(fā)之云函數(shù)的使用教程詳解
云函數(shù)是部署在云端的函數(shù),他和小程序本地的函數(shù)存在很大的區(qū)別。本文將通過(guò)示例詳細(xì)為大家介紹如何在小程序開(kāi)發(fā)中使用云函數(shù),需要的可以參考一下2022-03-03
JS數(shù)組循環(huán)的方式以及效率分析對(duì)比
本文詳細(xì)講解了JS數(shù)組循環(huán)的方式以及效率分析對(duì)比,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
JavaScript與C# Windows應(yīng)用程序交互方法
JavaScript與C# Windows應(yīng)用程序交互方法...2007-06-06
從柯里化分析JavaScript重要的高階函數(shù)實(shí)例
這篇文章主要為大家介紹了從柯里化分析JavaScript重要的高階函數(shù)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
javascript與CSS復(fù)習(xí)(《精通javascript》)
js和css結(jié)合來(lái)產(chǎn)生醒目的交互效果,我們可以快速的訪(fǎng)問(wèn)元素自身的樣式屬性2010-06-06
微信小程序?qū)崿F(xiàn)多選框全選與取消全選功能示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)多選框全選與取消全選功能,結(jié)合實(shí)例形式分析了微信小程序多選框功能實(shí)現(xiàn)、布局顯示及全選、取消全選相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
原生js實(shí)現(xiàn)可愛(ài)糖果數(shù)字時(shí)間特效
本文主要介紹了原生js實(shí)現(xiàn)可愛(ài)糖果數(shù)字時(shí)間特效的實(shí)例代碼,附效果展示和代碼演示。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2016-12-12

