【问题标题】:Selecting specific cells选择特定单元格
【发布时间】:2011-01-14 05:55:26
【问题描述】:
$('table.listings td:contains("You")').each(function(){
 $(this).children('td:nth-child(2)').addClass('highlighted');
});

我在页面上有多个table.listings,但选择了包含“你”的那个,我想将highlighted 添加到每一行的第二个单元格,但上面的代码没有按我预期的那样工作。

【问题讨论】:

    标签: jquery contains


    【解决方案1】:
    $('table.listings:contains("You") td:nth-child(2)').addClass("highlight");
    

    【讨论】:

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

      【讨论】:

        【解决方案3】:

        试试这个:

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

        【讨论】:

        • 这会做一些有时多余的事情,$('table.listings td:contains("You")'),如果同一个表包含更多带有“你”的<td>,那么里面的代码会为同一个表执行更多,做同样的事情事情。
        • 值得注意的是,提出问题的人也在这样做,所以“你”很可能只在表格的一部分,或者他/她不在乎它是否完成多次。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-11
        • 1970-01-01
        • 1970-01-01
        • 2013-09-02
        • 1970-01-01
        相关资源
        最近更新 更多