使用 JQuery 查找每个 <tr> 的最后一个 <td>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15024302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:14:36  来源:igfitidea点击:

Finding last <td> of each <tr> using JQuery

jquery

提问by Banala Ramu

I had two tables with same class name "Test", I need to find out last of each of first table using JQuery

我有两个具有相同类名“Test”的表,我需要使用 JQuery 找出每个第一个表的最后一个

Please help me out.

请帮帮我。

回答by jantimon

jQuery 1.1.4 introduced the last-child selector:

jQuery 1.1.4 引入了last-child 选择器

Description: Selects all elements that are the last child of their parent.

While :last matches only a single element, :last-child can match more than one: one for each parent.

描述:选择作为其父元素的最后一个子元素的所有元素。

虽然 :last 只匹配一个元素,但 :last-child 可以匹配多个:每个父元素一个。

$("table:first tr td:last-child");

回答by sasi

$('table:first tr').each(function() {
  var lasttd=  $(this).find(':last-child')
  //your code
});

回答by Anton

Loop through each tr and get the last td with :last

循环遍历每个 tr 并获得最后一个 td :last

$('table:first tr').each(function(){
$(this).find('td:last').addClass('LastTD');
});