jQuery select column
I wo开发者_开发知识库uld like to select all cells of the first column of a table. Can anyone please tell me the code.
Tried this..
$('.sortable tr:nth-child(1)'); // getting entire row.
This (fairly verbose) selector should work:
$(".sortable tr > :nth-child(1)")
If you want another column, simply change the index to nth-child
to something other than 1
.
This will select both td
(data) and th
(header) cells, btw.
$(".sortable tr > :nth-child(1)")
.css("background-color", "yellow");
<table class="sortable">
<tr> <th> A </th> <th> B </th> <th> C </th> </tr>
<tr> <td> 1 </td> <td> 2 </td> <td> 3 </td> </tr>
<tr> <td> 10 </td> <td> 20 </td> <td> 30 </td> </tr>
<tr> <td> 100 </td> <td> 200 </td> <td> 300 </td> </tr>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$('.sortable td:first-child');
$('.sortable tr td:first').each(function(){
alert($(this).text());
});
精彩评论