有条件的行格式化jQuery单元格在两个连续的行中具有不同的值



需要在连续行中染色与其他细胞不同。有条件的原始格式

假码将是:

foreach oddrow in table
    foreach cell in row
        if cellvalue != previosCellvalue(previous even)
              color differently

例如:

<table>
   <tr>
      <td>1</td><td>2</td>
   </tr>
      <td>1</td><td>3</td>
   <tr>
      <td>2</td><td>4</td>
   </tr>
   <tr>
      <td>2</td>4<td></td>
   </tr>
</table>

在上表中排一个单元格两个单元格,两个单元两个将被视为不同,其他所有将保留其样式。

您用jQuery标记了它,因此解决方案是:http://jsfiddle.net/kweny/

var last, inner;
$('table tr').each(function(i,tr) {
    inner = $.trim($(tr).html());
    if (inner == last) {
        $(tr).css('background-color', 'red');
    }
    last = inner;    
});​

很好。使用您的其他伪算法,您还可以使用以下方式检查每个第二行:http://jsfiddle.net/gbw49/

var last, inner;
$('table tr').each(function(i,tr) {
    inner = $.trim($(tr).html());
    if (inner == last && i % 2) {
        $(tr).css('background-color', 'red');
    }
    last = inner;    
});​

尝试使用

$('table tr td:nth-child(2)')

添加CSS的类似内容:

$(document).ready(function () {
        $('table tr td:nth-child(2)').css("background", "red");
        $('table tr td:nth-child(2)').css("color", "blue");
    });

,如果您只想为第1行和第2行执行此操作,请尝试

$(document).ready(function () {
         $('table tr:eq(0) td:nth-child(2)').css("background", "green");
         $('table tr:eq(1) td:nth-child(2)').css("background", "green");
    });

检查这个。我希望我有问题
http://jsfiddle.net/kweny/1/

    $('table tr:nth-child(even) td:eq(1)').each(function()
        {
            var  firstVal = $(this).text();
            var firstTd = $(this).closest("tr").prev().find("td:eq(1)");
             var secondVal = firstTd.text();
            if(firstVal != secondVal)
            {
                $(this).css({"background-color":"red"});
                firstTd. css({"background-color":"red"}); 
            }
        }
    );

最新更新