开发者

How to select child nodes but the first element

开发者 https://www.devze.com 2023-04-05 15:26 出处:网络
So I got a table and I want to set a style in all <tr> elements but the first. Example: <table>

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

精彩评论

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

关注公众号