jQuery选择表格中的列
微wx笑
2023-12-15【前端开发】
0
0关键字:
jQuery
以选择table中的第一列为例:方式1:1$("table tr").find("td:first").html("first column");//错误写法:$("table tr td:first").html("first column");方式2:1$("table tr").ea
以选择table中的第一列为例:
方式1:
1 | $("table tr").find("td:first").html("first column");//错误写法:$("table tr td:first").html("first column"); |
方式2:
1 | $("table tr").each(function(){ $(this).find("td:eq(0)").html("first column"); }); |
方式3:
1 | $("table tr td:first-child").html("first column"); |
方式4:
1 | $("table tr td:nth-child(1)").html("first column"); |
[扩展]
//选择最后一列
1 | $("table tr td:last-child").html("last column"); |
//选择奇数列
1 2 | $("table tr td:nth-child(odd)").html("odd colunms"); $("table tr td:nth-child(2n)").html("odd colunms"); |
//选择偶数列
1 2 | $("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
本文为转载文章,版权归原作者所有,不代表本站立场和观点。



