我们可以通过document.getElementById('tb').childNodes[0]获得tbody对象,
然后通过tbody.length进行遍历,如tbody.chilNode[i].
也可以直接获得rows数组遍历,如document.getELementById('tb').rows.
对于行我们可以通过sectionRowIndex和rowIndex获得位置.
rowIndex:
Retrieves the position of the object in the rows collection for the table.
sectionRowIndex:
Retrieves the position of the object in the tBody, tHead, tFoot, or rows collection.
代码如:
[code]
function a(par) {
var trow=document.getElementById(par).rows;
var l=trow.length;
for(var i=0;i<l;i++) {
if(trow[i].rowIndex%2==0)
trow[i].style.backgroundColor="#F2F2F2";
else
trow[i].style.backgroundColor="#ffffff"
}
}
[/code]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>
</head>
<body>
<table border="0" width="500" cellpadding=0 cellspacing="1" bgcolor=#0099FF align=center id="tb">
<tr>
<td> 1</td><td> 1</td><td> 6</td>
</tr>
<tr>
<td> 2</td><td> 2</td><td> 6</td>
</tr>
<tr>
<td> 3</td><td> 3</td><td> 6</td>
</tr>
<tr>
<td> 4</td><td> 4</td><td> 6</td>
</tr>
<tr>
<td> 5</td><td> 5</td><td> 6</td>
</tr>
<tr>
<td> 6</td><td> 6</td><td> 6</td>
</tr>
</table>
<script defer>
function a(par) {
var trow=document.getElementById(par).rows;
var l=trow.length;
for(var i=0;i<l;i++) {
if(trow[i].rowIndex%2==0)
trow[i].style.backgroundColor="#F2F2F2";
else
trow[i].style.backgroundColor="#ffffff"
}
}
a("tb")
</script>
</body>
</html>
2.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>
</head>
<body>
<table border="0" width="500" cellpadding=0 cellspacing="1" bgcolor=#0099FF align=center id="tb">
<tr>
<td> 1</td><td> 1</td><td> 6</td>
</tr>
<tr>
<td> 2</td><td> 2</td><td> 6</td>
</tr>
<tr>
<td> 3</td><td> 3</td><td> 6</td>
</tr>
<tr>
<td> 4</td><td> 4</td><td> 6</td>
</tr>
<tr>
<td> 5</td><td> 5</td><td> 6</td>
</tr>
<tr>
<td> 6</td><td> 6</td><td> 6</td>
</tr>
</table>
<script defer>
function a(par) {
//var tbody=document.getElementById(par).childNodes[0].childNodes;
var tbody=document.getElementById(par).rows;
var l=tbody.length;
for(var i=0;i<l;i++) {
if(tbody[i].sectionRowIndex%2==0)
tbody[i].style.backgroundColor="#F2F2F2";
else
tbody[i].style.backgroundColor="#ffffff"
}
}
a("tb")
</script>
</body>
</html>
posted on 2006-10-28 13:55
汪杰 阅读(376)
评论(0) 编辑 收藏 引用 所属分类:
js+dhtml