如何使用.each()和.last()在每个tr中选择最后一个td? [英] How to select the last td in every tr using .each() and .last()?

查看:301
本文介绍了如何使用.each()和.last()在每个tr中选择最后一个td?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用.each()循环和.last()方法选择每个tr中的最后一个td.

I want to select the last td in every tr using the .each() loop and .last() method.

这就是我在做什么:

$('tr:has(td)').children().each(function(){return $(this).parent().last()});

但是该代码返回每个td,而不仅仅是每行的最后一个.

But that code returns every td and not just the last one from each row.

有人可以告诉我如何解决吗?

Can someone tell me how to fix it?

谢谢.

推荐答案

您只需编写

$('tr > td:last-child')

您的代码有误的原因有很多:

Your code is wrong for a number of reasons:

  • .last()选择当前集中的最后一个元素;您需要.children().last()
  • .each()对其回调的返回值不执行任何操作;您想要.map()
  • children()使您的回调针对每一行的每个单元格运行.
  • .last() selects the last element in the current set; you need .children().last()
  • .each() doesn't do anything with the return value of its callback; you want .map()
  • children() makes your callback run for every cell in each row.

这篇关于如何使用.each()和.last()在每个tr中选择最后一个td?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆