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

mac2022-06-30  15

主要思路:

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);        }    }}

转载于:https://www.cnblogs.com/Mblog/archive/2009/11/27/1612089.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)