javascript實(shí)現(xiàn)表單隔行變色
本文實(shí)例為大家分享了javascript實(shí)現(xiàn)表單隔行變色的具體代碼,供大家參考,具體內(nèi)容如下
效果如下:
代碼思路:
1、用到鼠標(biāo)經(jīng)過(guò)onmouseover 鼠標(biāo)離開(kāi)onmouseout
2、鼠標(biāo)經(jīng)過(guò)tr行,當(dāng)前的行變背景顏色,鼠標(biāo)離開(kāi)去掉當(dāng)前的背景顏色
3、注意:第一行 th不需要變色, 只用獲得有td的行,這里我們用thead和tbody進(jìn)行區(qū)分
代碼如下:
html部分:
<table border="1" cellpadding="10px" rules="all"> ?? ??? ??? ?<thead> ?? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ?<th>one</th> ?? ??? ??? ??? ??? ?<th>two</th> ?? ??? ??? ??? ??? ?<th>three</th> ?? ??? ??? ??? ??? ?<th>four</th> ?? ??? ??? ??? ?</tr> ?? ??? ??? ?</thead> ?? ??? ??? ?<tbody> ? ?? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ?<td>第一行</td> ?? ??? ??? ??? ??? ?<td>第一行</td> ?? ??? ??? ??? ??? ?<td>第一行</td> ?? ??? ??? ??? ??? ?<td>第一行</td> ?? ??? ??? ??? ?</tr> ?? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ?<td>第二行</td> ?? ??? ??? ??? ??? ?<td>第二行</td> ?? ??? ??? ??? ??? ?<td>第二行</td> ?? ??? ??? ??? ??? ?<td>第二行</td> ?? ??? ??? ??? ?</tr> ?? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ?<td>第三行</td> ?? ??? ??? ??? ??? ?<td>第三行</td> ?? ??? ??? ??? ??? ?<td>第三行</td> ?? ??? ??? ??? ??? ?<td>第三行</td> ?? ??? ??? ??? ?</tr> ?? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ?<td>第四行</td> ?? ??? ??? ??? ??? ?<td>第四行</td> ?? ??? ??? ??? ??? ?<td>第四行</td> ?? ??? ??? ??? ??? ?<td>第四行</td> ?? ??? ??? ??? ?</tr> ?? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ?<td>第五行</td> ?? ??? ??? ??? ??? ?<td>第五行</td> ?? ??? ??? ??? ??? ?<td>第五行</td> ?? ??? ??? ??? ??? ?<td>第五行</td> ?? ??? ??? ??? ?</tr> ?? ??? ??? ?</tbody> </table>
css部分:
添加一個(gè)類(lèi),使鼠標(biāo)放上去的時(shí)候變色
<style> ?? ??? ??? ?.bg { ?? ??? ??? ??? ?background-color: pink; ?? ??? ??? ?} </style>
javascript部分:
//獲取元素 // 獲取tbody里面的所有的行 ?? ??? ?var trs = document.querySelector("tbody").querySelectorAll("tr"); ? ?? ??? ?//鼠標(biāo)經(jīng)過(guò)時(shí)間 ?? ??? ?for (var i = 0; i < trs.length; i++) { ?? ??? ??? ?trs[i].onmouseover = function() { ?? ??? ??? ??? ?this.className = 'bg' ?? ??? ??? ?} ?? ??? ??? ?//鼠標(biāo)離開(kāi)事件 ?? ??? ??? ?trs[i].onmouseout=function(){ ?? ??? ??? ??? ?this.className=''; ?? ??? ??? ?} ?? ??? ?}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在IE6下發(fā)生Internet Explorer cannot open the Internet site錯(cuò)誤
最近在IE6下面時(shí)常會(huì)發(fā)生“Internet Explorer cannot open the Internet site ”錯(cuò)誤。2010-06-06JavaScript算法教程之sku(庫(kù)存量單位)詳解
這篇文章主要給大家介紹了JavaScript算法教程之sku(庫(kù)存量單位)的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-06-06JS中頁(yè)面與頁(yè)面之間超鏈接跳轉(zhuǎn)中文亂碼問(wèn)題的解決辦法
在原頁(yè)面一張圖片上添加了一個(gè)鏈接,鏈接中有中文,于是在跳轉(zhuǎn)過(guò)程中出現(xiàn)中文亂碼問(wèn)題,下面給大家分享下解決方案2016-12-12JavaScript實(shí)現(xiàn)QueryString獲取GET參數(shù)的方法
本文為大家詳細(xì)介紹下如何通過(guò)JavaScript實(shí)現(xiàn)QueryString獲取GET參數(shù),具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07JavaScript動(dòng)態(tài)設(shè)置div的樣式的方法
這篇文章主要介紹了JavaScript動(dòng)態(tài)設(shè)置div的樣式的方法的相關(guān)資料,需要的朋友可以參考下2015-12-12js實(shí)現(xiàn)無(wú)刷新監(jiān)聽(tīng)URL的變化示例代碼詳解
這篇文章主要介紹了js如何無(wú)刷新監(jiān)聽(tīng)URL的變化,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06JavaScript實(shí)現(xiàn)簡(jiǎn)單的時(shí)鐘實(shí)例代碼
這篇文章主要介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單的時(shí)鐘實(shí)例代碼,有需要的朋友可以參考一下2013-11-11使用PBFunc在Powerbuilder中支付寶當(dāng)面付款功能
這篇文章主要介紹了使用PBFunc在Powerbuilder中支付寶當(dāng)面付款功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10