当前位置:编程学习 > JAVA >>

javascript table 隔行变色

从此再也不用为了隔行变色而引入jquery了
[javascript] 
function changeTableColor(oddColor,evenColor){ 
    var tables=document.getElementsByTagName('table'); 
    for(var i=0;i<tables.length;i++){ 
        var table=tables[i]; 
        var childs=table.childNodes; 
        for(var j=0;j<childs.length;j++){ 
            var child=childs[j]; 
            if(child.nodeName=='TBODY'){ 
                var trs=child.childNodes; 
                for(var x=0;x<trs.length;x++){ 
                    if(trs[x].nodeName=='TR'){ 
                        var tr=trs[x]; 
                        if(x%4==0 ){ 
                            trs[x].style.backgroundColor=oddColor; 
                        } 
                        if(x%4==2){ 
                            trs[x].style.backgroundColor=evenColor; 
                        } 
                    } 
                } 
            } 
        } 
    } 

[javascript] view plaincopy
window.onload=function(){ 
    changeTableColor('#ddd','#eee'); 

 

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,