一段自动给表格增加双色显示的JS(带思路说明)

主要思路:

1.页面加载结束后获取当前页面所有的表格。

2.遍历表格的所有行。

3.修改行的样式,奇数行是'odd' 样式,偶数行是'even'样式。

4.给每一行增加onmouseover 事件,事件发生时改变当前行的样式。

function add_event(tr){
    tr.onmouseover = function(){
        tr.className += 'hover';
    };
    tr.onmouseout = function(){
    tr.className = tr.className.replace('hover', '');
    };
}

function stripe(table) {
    var trs = table.getElementsByTagName("tr");
    for(var i=1; i<trs.length; i++){
        var tr = trs[i];
        tr.className = i%2 != 0? 'odd' : 'even';
        add_event(tr);
    }
}

window.onload = function(){
    var tables = document.getElementsByTagName('table');
    for(var i=0; i<tables.length; i++){
        var table = tables[i];
        if(table.className == 'datagrid1' || table.className == 'datagrid2'|| table.className == 'datagrid3'){
            stripe(table);
        }
    }
}

posted @ 2009-11-27 15:39  溪水云天  阅读(355)  评论(0编辑  收藏  举报