gpt4 book ai didi

jquery - 如何使用Jquery选择数据表中的一列

转载 作者:行者123 更新时间:2023-11-28 01:51:12 25 4
gpt4 key购买 nike

我发现一个解决方案是浏览表中的每一行。但是有更好的解决方案吗?

$(document).ready(function() {
$('table tr').each(function(){
$(this).find('td').eq(2).css('background-color', 'green');
});
});

最佳答案

您不需要使用each() 方法。只需使用jquery 选择器

$('tr td:first-child').css('background','#07C');

或第n列

$('tr td:nth-child(2)').css('background','#07C');

$(function() {
$('tr td:nth-child(2)').css('background','#5ae');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
<tr>
<td>first Column</td>
<td>second Column</td>
<td>third Column</td>
</tr>
<tr>
<td>first Column</td>
<td>second Column</td>
<td>third Column</td>
</tr>
<tr>
<td>first Column</td>
<td>second Column</td>
<td>third Column</td>
</tr>
</table>

关于jquery - 如何使用Jquery选择数据表中的一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49916502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com