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

条件行格式化两个连续行中具有不同值的jQuery单元格,jquery,Jquery,需要为连续行中的单元格着色,其格式不同于其他单元格。条件原始格式化 伪PCode类似于: foreach oddrow in table foreach cell in row if cellvalue != previosCellvalue(previous even) color differently 例如: <table> <tr> <td>1</td><td&g

需要为连续行中的单元格着色,其格式不同于其他单元格。条件原始格式化

伪PCode类似于:

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>

12
13
24
24
在上表中,第1行单元格2和第2行单元格2的样式将不同,所有其他单元格将保留其样式。

尝试使用

$('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");
    });

您使用jQuery对其进行了标记,因此解决方案是:

嗯。使用附加的伪算法,您还可以使用以下命令仅检查第二行:


检查这一个。我希望我的问题没有弄错


问题是什么?你尝试了什么?我已经更新了。我可以用Javascript来实现,但正如我逐渐意识到的那样,总会有一个更简单、更干净的jQuery解决方案。
var last, inner;
$('table tr').each(function(i,tr) {
    inner = $.trim($(tr).html());
    if (inner == last) {
        $(tr).css('background-color', 'red');
    }
    last = inner;    
});​
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: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"}); 
            }
        }
    );