Jquery 选择特定的细胞

Jquery 选择特定的细胞,jquery,contains,Jquery,Contains,我在页面上有多个table.listings,但包含您的列表已被选中,我希望将突出显示的类添加到每行的第二个单元格中,但上面的代码无法按预期工作。尝试以下操作: $('table.listings td:contains("You")').each(function(){ $(this).children('td:nth-child(2)').addClass('highlighted'); }); 这会做一些有时是多余的事情,$'table.listings td:containsYou'

我在页面上有多个table.listings,但包含您的列表已被选中,我希望将突出显示的类添加到每行的第二个单元格中,但上面的代码无法按预期工作。

尝试以下操作:

$('table.listings td:contains("You")').each(function(){
 $(this).children('td:nth-child(2)').addClass('highlighted');
});

这会做一些有时是多余的事情,$'table.listings td:containsYou',如果同一个表包含更多的,那么在同一个表中执行更多的代码,做同样的事情。值得注意的是,提出问题的人也在这样做,所以很可能你只在表的一部分,或者他/她不在乎是否做了多次。
$('table.listings td:contains("You")').each(function(){
    $("td:nth-child(2)", $(this).parent().parent().get(0)).addClass('highlighted');
});
$('table.listings :contains("You")').each(function(){
 $(this).children('td:nth-child(2)').addClass('highlighted');
});
$('table.listings:contains("You") td:nth-child(2)').addClass("highlight");