The code in the first Code Block is the source code. After the source code is running. The code will be changed (See the 2nd Code Block).
I want the class(i.e. ‘even’ and ‘odd’) of tag is only displayed in the ‘table1’. However, currently a nest table (i.e. ‘table2’) also has ‘even’ and ‘odd’ class of each tag.
Can anyone help me out? Thanks in advance.
———–First Code Block————–
<head>
<script type="text/javascript">
$(document).ready(function(){
$("#table1 tr:odd").addClass("odd");
$("#table1 tr:not(.odd)").addClass("even");
});
</script>
</head>
<body>
<table id="table1">
<tr>
<td>AAA</td>
<td>CCC</td>
</tr>
<tr>
<td>BBB</td>
<td>DDD</td>
</tr>
<tr>
<td>
<table id="table2">
<tr></tr>
<tr></tr>
<table>
</td>
</tr>
</table>
</body>
———–2nd Code Block—————
<table id="table1">
<tr class="even">
<td>AAA</td>
<td>CCC</td>
</tr>
<tr class="odd">
<td>BBB</td>
<td>DDD</td>
</tr>
<tr class="even">
<td>
<table id="table2">
<tr class="even"></tr>
<tr class="odd"></tr>
<table>
</td>
</tr>
</table>
All of the posted answer are almost right..
Many browsers automatically add a
tbodyto your table even if you don’t add one yourself. So#table1 > trwill not match becausetris not a direct child oftable. Your best bet is to use the above and explicitly add atbodyfor those browsers that don’t do it for you.http://jsfiddle.net/5ETAD/1/