jQuery-如何选择表中所有行的最后一列? [英] jQuery - How to select last column of all rows in a table?
问题描述
假设我有一个HTML表格,如下所示(带有相应的tr,td标签):
Assuming I have a HTML table as follows (with the appropriate tr, td tags):
a1 | b1 | c1
a2 | b2 | c2
a3 | b3 | c3
a4 | b4 | c4
<table border="1">
<tr>
<td>a1</td>
<td>b1</td>
<td>c1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
<td>c2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
<td>c3</td>
</tr>
<tr>
<td>a4</td>
<td>b4</td>
<td>c4</td>
</tr>
</table>
我正在尝试选择所有c *行以对其执行操作.
I'm trying to select all the c* rows to perform an action on them.
使用$('td:last')
选择器仅选择最后一个td标签,即c4.
Using the $('td:last')
selector just selects the last td tag, i.e., c4.
我尝试了$('tr > td:last')
,该方法不起作用.在这种情况下,正确的选择器是什么?
I tried $('tr > td:last')
which didn't work. What would be the correct selector in this case?
推荐答案
:last
是jQuery选择器,但只会选择该类型的最后一个元素.您最有可能在寻找:last-child
.
:last
is a jQuery selector, but it will only select the very last element of that type. You're most likely looking for :last-child
.
类似地,td:first-child
将为您提供第一个单元格,而:first
将为您提供该类型的第一个元素.
Similarly, td:first-child
will get you the first cell, while :first
gets you the first element of that type.
这是一个小提琴,供您查看所有四个示例.
Here's a Fiddle for you to look at with all four examples.
这篇关于jQuery-如何选择表中所有行的最后一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!