Forum Moderators: not2easy
Is it possible to set the style class of a row dynamically?
Actually I want to apply different style for each rows .
for example:my first row bgcolor is green ,my second row bgcolor is red,
my third row bgcolor is blue and so on...
lam doesn't know how many rows a table has because it is dynamic.
tbody:nth-child(3n+1){background-color:#0f0;}
tbody:nth-child(3n+2){background-color:#f00;}
tbody:nth-child(3n){background-color:#00f;}
But afaik, support is limited to Konqueror 3.4+ ([en.wikipedia.org ])
I guess it's time for everybody to move to Linux. ;)
tr:nth-child(3n) { background-color: red; }
etc. etc. element:nth-child(n) selects an element when it is the nth-child, not the nth-child of an element.
It looks like the next release of Opera [my.opera.com] will support this syntax as well, which will be nice.
Thanks for the correction; I guess the "child" part threw me off. The funny thing is, a week or two ago I was experimenting and wrote a test page for nth-child, and there I used it correctly.
I suppose if I really wanted to use tbody instead on tr I could write:
tbody>*:nth-child... :)
Good new about Opera; it's a wonderful browser and I really like it, but I almost never seem to use. Go figure.
<html>
<head><script language="javascript">
<!--
var tableRowColors = ['#00FF00','#FF0000','#0000FF'];function colorTableRows(table_id) {
if (!document.getElementById(table_id)) return false;
var table = document.getElementById(table_id);
var rows = table.getElementsByTagName('TR');
var currentColorIndex = 0;
for (var i=0;i<rows.length;i++) {
if (currentColorIndex == tableRowColors.length) currentColorIndex = 0;
rows[i].style.backgroundColor = tableRowColors[currentColorIndex];
currentColorIndex++;
}
}
//-->
</script></head>
<body onload="colorTableRows('test_table');">
<table id="test_table">
<tr><td>hello</td></tr>
<tr><td>world</td></tr>
<tr><td>how</td></tr>
<tr><td>are</td></tr>
<tr><td>you?</td></tr>
</table></body>
</html>