两种:第n个孩子和:最后一个孩子

问题描述:

代码:两种:第n个孩子和:最后一个孩子

<table> 
    <tr> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td colspan="2"></td> <==== Only this 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td colspan="3"></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td></td> 
    </tr> 
</table> 

我只想选择第四个“TD”,但只有当它的最后一次。我可以这样做吗?

tr td:last-child:nth-child(4) { /* Some Style */ } 
+5

这应该像你一样工作。 – thirtydot

+1

是的,你可以:[** DEMO **](http://jsfiddle.net/K9TF8/) – MarcinJuraszek

+3

这是怎么回事?要么在发布问题之前没有尝试自己的代码,要么通过简化太多而将实际问题抽象出来。 – thirtydot

是的,你可以

tr td:last-child:nth-child(4) { background: red; } 

我想是不是possible.If你想那么它的工作,你可以添加特定classid与你的愿望td元素。

试试这个

$("tr").each(function(){ 
    if($(this).find("td:nth-child(4)").is(':last-child')) { 
    $(this).find("td:nth-child(4)").css("color","red"); 
    } 
}); 

FIDDLE

+1

在这个问题上有一些看不见的jQuery标签吗? – thirtydot

您可以使用这样demo

tr td:nth-of-type(4):last-child { 
    /*some code here*/ 
    } 

或者

tr td:nth-child(4):last-child { 
    background-color: red; 
    }