使用jQuery在每个表行的第一个TD上添加类

前端之家收集整理的这篇文章主要介绍了使用jQuery在每个表行的第一个TD上添加类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一张我正在使用的桌子,就像这样:
  1. <table width="100%" border="0" cellspacing="0" cellpadding="0" id="tablecontent">
  2. <tr class="tablerow">
  3. <td>This is the td I want to add a class to.</td>
  4. <td class="cell2">Stuff</td>
  5. <td class="cell3">Stuff</td>
  6. </tr>
  7. <tr class="tablerow">
  8. <td>This is the td I want to add a class to.</td>
  9. <td class="cell2">Stuff</td>
  10. <td class="cell3">Stuff</td>
  11. </tr>
  12. </table>@H_404_3@
  13. 每行中的第一个TD标记没有要使用的类或ID.我没有权限更改HTML输出,所以我想添加一些jQuery来定位每个tablerow的第一个TD标记.我该怎么办?

解决方法

  1. $('#tablecontent td:first-child').addClass('someClass');@H_404_3@
  2. 这使用the first-child selector来选择所有< td> #tablecontent表中的元素是其父元素的第一个子元素.

  3. 示例:http://jsfiddle.net/duKKC/

猜你在找的jQuery相关文章