开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜