选择每行带有jQuery的第一个TD

| 如何为表格中每一行的每个第一个单元格分配样式?
$(\"#myTable tr td:first\").addClass(\"black\");
    
已邀请:
        使用
:first-child
伪类而不是
:first
$(\"#myTable tr td:first-child\").addClass(\"black\");
:first
伪类实际上选择了列表中返回的第一个元素。例如,“ 5”将仅返回碰巧返回的第一个div下的第一个跨度。
:first-child
伪类选择特定父级下的第一个元素,但返回的元素与第一个子级一样多。例如,
$(\'table tr td:first-child\')
返回每行的第一个单元格。 当您使用
:first
时,它仅返回碰巧被选中的第一行的第一个单元格。 有关更多信息,请查阅jQuery文档: http://api.jquery.com/first-selector/ http://api.jquery.com/first-child-selector/     
        您非常接近,我认为您所需要的只是
:first-child
而不是
:first
,所以这样的事情:
$(\"#myTable tr td:first-child\").addClass(\"black\");
    
        像这样:
$(\"#myTable tr\").each(function(){
    $(this).find(\'td:eq(0)\').addClass(\"black\");
});
    
        
$(\"#myTable tr\").find(\"td:first\").addClass(\"black\");
    
        尝试:
$(\"#myTable td:first-child\").addClass(\"black\");
    

要回复问题请先登录注册