jQuery选择表格中的列

以选择table中的第一列为例:
方式1:

$("table tr").find("td:first").html("first column");//错误写法:$("table tr td:first").html("first column");

方式2:

$("table tr").each(function(){ $(this).find("td:eq(0)").html("first column");  });

方式3:

$("table tr td:first-child").html("first column"); 

方式4:

$("table tr td:nth-child(1)").html("first column"); 

[扩展]

//选择最后一列

$("table tr td:last-child").html("last column"); 

//选择奇数列

$("table tr td:nth-child(odd)").html("odd colunms"); 
$("table tr td:nth-child(2n)").html("odd colunms"); 

//选择偶数列

$("table tr td:nth-child(even)").html("even colunms"); 
$("table tr td:nth-child(2n-1)").html("even colunms");

相关链接

详解CSS中:nth-child的用法

原文地址:https://www.cnblogs.com/njl041x/p/4110611.html