0

需要以不同于其他单元格的不同格式对连续行中的单元格进行着色。条件原始格式

伪代码将类似于:

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>

在上面的表格中,第一行第二单元格和第二行第二单元格的样式将不同,所有其他将保留其样式。

4

3 回答 3

2

你用 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;    
});​
于 2012-11-09T18:27:29.963 回答
0

尝试使用

$('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");
    });
于 2012-11-09T18:19:01.047 回答
0

检查这个。我希望我的问题正确
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"}); 
            }
        }
    );
于 2012-11-09T18:57:43.390 回答