How can I add a class to the first and second td in each tr?
<div class='location'>
<table>
<tbody>
<tr>
<td>THIS ONE</td>
<td>THIS ONE</td>
<td>else</td>
<td>here</td>
</tr>
<tr>
<td>THIS ONE</td>
<td>THIS ONE</td>
<td>else</td>
<td>here</td>
</tr>
</tbody>
</table>
</div>
For the first td, this开发者_开发技巧 does nothing?
$(".location table tbody tr td:first-child").addClass("black");
Can I also use second-child?
$(".location table tbody tr td:first-child").addClass("black");
$(".location table tbody tr td:nth-child(2)").addClass("black");
http://jsfiddle.net/68wbx/1/
To select the first and the second cell in each row, you could do this:
$(".location table tbody tr").each(function() {
$(this).children('td').slice(0, 2).addClass("black");
});
You can do in this way also
var prop = $('.someProperty').closest('tr');
If the number of tr is in array
$.each(prop , function() {
var gotTD = $(this).find('td:eq(1)');
});
$(".location table tbody tr").each(function(){
$('td:first', this).addClass('black').next().addClass('black');
});
another:
$(".location table tbody tr").find('td:first, td:nth-child(2)').addClass('black');
If you want to add a class to the first and second td you can use .each()
and slice()
$(".location table tbody tr").each(function(){
$(this).find("td").slice(0, 2).addClass("black");
});
Example on jsfiddle
jquery provides one more function: eq
Select first tr
$(".bootgrid-table tr").eq(0).addClass("black");
Select second tr
$(".bootgrid-table tr").eq(1).addClass("black");
You can just pick the next td:
$(".location table tbody tr td:first-child").next("td").addClass("black");
精彩评论