简体   繁体   English

jquery如何选择每个表行的第一个单元格

[英]jquery How to select the 1st cell of each table row

I have a table like this: 我有这样一张桌子:

<table id="myTable">
   <tr><td>1sta</td><td>2nd</td></tr>
   <tr><td>1stb</td><td>2nd</td></tr>
   <tr><td>1stc</td><td>2nd</td></tr>
   <tr><td>1std</td><td>2nd</td></tr>
</table>

Using jQuery How do I select the 1st <td> element in each row of "myTable"? 使用jQuery如何在“myTable”的每一行中选择第一个<td>元素?

The first-child selector grabs each td that is the first child of its parent (the tr in this case). first-child选择器抓取每个td ,它是其父级的第一个子级(在本例中为tr )。

$('#myTable td:first-child');

http://api.jquery.com/first-child-selector/ http://api.jquery.com/first-child-selector/

Use this selector '#myTable td:first-child' . 使用此选择器'#myTable td:first-child'

That will select the first td for every tr . 这将为每个 tr选择第一个td Avoid the temptation to use :first instead of :first-child . 避免使用的诱惑:first而不是:first-child :first will only select a single element. :first只选择一个元素。 In this case it would be the first cell of the first row. 在这种情况下,它将是第一行的第一个单元格。

http://api.jquery.com/first-child-selector/ http://api.jquery.com/first-child-selector/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM