选择表列(jQuery)

我想给FIRST和LAST列的所有(包括

)单元格一些特定的类,我试过这个:

 $("table tr:first-child td:first-child").addClass("first-col-cell"); $("table tr:last-child td:last-child").addClass("last-col-cell"); 

..但它似乎不起作用。

非常感谢任何帮助。

编辑:你的选择器非常接近:

  

如果元素很重要……不确定你是怎么想对待colspans的。

  
blah
blah
blah

试着把它分解一下,你正在做的遍历是不正确的

 // This will take the first child which is a TD, within any TR in the table. $("table tr td:first-child").addClass("first-col-cell"); // This will take the first child which is a TR within the table. $("table tr:first-child").addClass("first-col-cell"); // Just like the above, except now we're doing the last occurances $("table tr td:last-child").addClass("last-col-cell"); $("table tr:last-child").addClass("last-col-cell"); 

然后我们只需要确保标记是好的

 
1 1 1 1
2 2 2 2
3 3 3 3

然后jQuery应该通过以下结果遍历每个

 
1 1 1 1
2 2 2 2
3 3 3 3