我有一个表结构:
<table id="table1">
<thead>
<tr>
<th>Task</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>task 1</td>
<td>SUC</td>
</tr>
<tr>
<td>task 2</td>
<td>SUC</td>
</tr>
<tr>
<td>task 3</td>
<td>ERR</td>
</tr>
<tr>
<td>task 4</td>
<td>ERR</td>
</tr>
我想检查所有任务的状态,如果状态为 ERR,那么我必须更改该行的 CSS。为此,我有一个像这样的 jquery:
$('#table1 td:nth-child(2)').each(function() {
alert($(this).html());
if ($(this).html() == 'ERR') {
$(this).css('background-color', '#ffe83d');
}
它只是更改列 Status 的特定单元格的 css。
我想更改状态为 ERR 的整行的 css。 我曾尝试过类似的东西
$(this).parent().css('background-color', '#59f763');
最佳答案
试试这个:使用 closest('tr')
$('#table1 td:nth-child(2)').each(function() {
alert($(this).html());
if ($(this).html() == 'ERR') {
$(this).closest('tr').css('background-color', '#ffe83d');
}
});
关于Jquery:遍历一个表来找到一个值的出现并更改行css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31399181/