如何遍历列并将其单元格设置为类jquery
我想循环我表格的第一列,将单元格设置为红色(如果是奇数),蓝色(如果是偶数)。 在下面的例子中:包含a的单元格,c是红色的,b是蓝色的如何遍历列并将其单元格设置为类jquery,jquery,Jquery,我想循环我表格的第一列,将单元格设置为红色(如果是奇数),蓝色(如果是偶数)。 在下面的例子中:包含a的单元格,c是红色的,b是蓝色的 <table class='sortable'> <tr> <td>a</td> <td>d</td> <td>g</td> </tr> <tr>
<table class='sortable'>
<tr>
<td>a</td>
<td>d</td>
<td>g</td>
</tr>
<tr>
<td>b</td>
<td>e</td>
<td>h</td>
</tr>
<tr>
<td>c</td>
<td>f</td>
<td>i</td>
</tr>
</table>
使用此代码仅设置每行的第一列。不需要循环:
$(".sortable tr:even td:first-child").css("color", "blue");
$(".sortable tr:odd td:first-child").css("color", "red");
如果要使用类并仅使用上面列出的表执行此操作:
$(document).ready(function() {
$(".sortable tr:odd td:first").addClass("red");
$(".sortable tr:even td:first").addClass("blue");
});
或
如果要编辑多行:并且不知道带a、b、c的列是第一行还是第二行,或者根本不知道:
$(document).ready(function() {
$(".sortable tr td:first).each(function(){
if ($(this).text()=="a" || $(this).text()=="c"){
$(this).addClass("red");
}
if ($(this).text()=="b"){
$(this).addClass("blue");
}
}
});
您还需要指定您的类
<style>
.blue {color:blue}
.red {color:red}
</style>
记住,据我所知,它必须是第一个应该接收css的TD,而不是整行。Ziga是正确的,它应该是$。可排序TD:奇数和$。可排序TD:偶数
<style>
.blue {color:blue}
.red {color:red}
</style>