How to select child nodes but the first element
So I got a table and I want to set a style in all <tr>
elements but the first. Example:
<table>
<tr><th>This receives header styles</th></tr>
<tr><td>This receives item styles</td></tr>
<tr><td>This receives开发者_如何学Go item styles</td></tr>
<tr><td>This receives item styles</td></tr>
</table>
I know I have to use selectors but I don't know how and can't find it on the web =S
The general sibling selector ~
lets you select every certain element that comes after another. You can use this selector:
table tr:first-child ~ tr
Works in IE7+.
If you can modify your HTML to make it more semantic, you can place your first row with the th
cells in a thead
, and the rest in a tbody
:
<table>
<thead>
<tr><th>This receives header styles</th></tr>
</thead>
<tbody>
<tr><td>This receives item styles</td></tr>
<tr><td>This receives item styles</td></tr>
<tr><td>This receives item styles</td></tr>
</tbody>
</table>
Then use this selector instead:
table tbody tr
Works on older browsers in case you need it.
$('#table-id tr').not(':first-child').addClass('class');
should do it
精彩评论