English 中文(简体)
使用jQuery将类添加到每个表行的第一个TD
原标题:Using jQuery to add class to first TD on each table row

我有一张正在工作的桌子,它是这样的:

<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>

每行中的第一个TD标记没有可使用的类或ID。我没有更改HTML输出的权限,所以我想添加一点jQuery来针对每个表行的第一个TD标记。我该怎么做?

最佳回答
$( #tablecontent td:first-child ).addClass( someClass );

这使用第一个子选择器,以选择#tablecontent表中的所有<;td>;元素,这些元素是其父级的第一个子

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

问题回答

你可以试试下面的jQuery

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

这将解决您的问题:)

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

我相信以下几点会奏效:

$( .tablerow td:first-child ).addClass( class );
$( #tablecontent td:first-child ).addClass("first-row");




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签