开发者

Using jQuery to add class to first TD on each table row

I have a table I'm working with that is like such:

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tablecontent">
  <tr class="tablerow">
    <td>This is the td I want to add a class to.</td>
    <td class="cell2">Stuff开发者_运维技巧</td>
    <td class="cell3">Stuff</td>
  </tr>
  <tr class="tablerow">
    <td>This is the td I want to add a class to.</td>
    <td class="cell2">Stuff</td>
    <td class="cell3">Stuff</td>
  </tr>
</table>

The first TD tag in each row does not have a class or ID to work with. I don't have access to change the HTML output so I figured to add in a bit of jQuery to target the first TD tag of each tablerow. How would I do this?


$('#tablecontent td:first-child').addClass('someClass');

This uses the first-child selector to select all <td> elements in the #tablecontent table that are a first-child of their parent.

Example: http://jsfiddle.net/duKKC/


You can try the below jQuery

$('.tablerow').each(function(index) {
    $(this).children('td').first().addClass('class');
});

This will solve your problem :)


$(".tablerow").find("td:first-child").addClass('class');


I believe the following would work:

$('.tablerow td:first-child').addClass('class');


$('#tablecontent td:first-child').addClass("first-row");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜