我有一个html <table>
显示日志,我想循环整个表格,突出显示具有不同值的行中的任何相邻单元格<tr>
。
我正在尝试比较特定行中<td>
中的任意两个值。我已经设法做了一些事情,但只有2列。
下面是表格结构的html示例代码:
<table id="coa_history_data">
<tr>
<th>old Name</th>
<th>New Name</th>
<th>Old Phone</th>
<th>New Phone</th>
<th>Old Age</th>
<th>New Age</th>
</tr>
<tbody>
<tr class="data-in-table">
<td>Alphy</td>
<td>Alphy</td>
<td>015</td> //should be highlited
<td>016</td> //should be highlited
<td>23</td> //should be highlited
<td>24</td> //should be highlited
</tr>
<tr>
<td>Tom</td>
<td>Tom</td>
<td>12</td>
<td>12</td>
<td>65</td> //should be highlited
<td>30</td> //should be highlited
</tr>
<tr>
<td>will</td>
<td>will</td>
<td>45</td>
<td>45</td>
<td>20</td>
<td>20</td>
</tr>
</tbody>
</table>
我的JavaScript代码:
$("#coa_history_data tbody tr.data-in-table").each(function() {
var $firstCell = $('td:eq(3)', this);
var $thirdCell = $('td:eq(4)', this);
if($firstCell.text() === $thirdCell.text()){
}else{
$firstCell.css('backgroundColor','yellow');
$thirdCell.css('backgroundColor','yellow');
}
});
我想提出一些建议,如何处理?
答案 0 :(得分:3)
我建议如下所示,以便独立于列数。
<强> JS 强>
$("#coa_history_data tbody tr.data-in-table").each(function () {
$(this).find('td').each(function (index) {
var currentCell = $(this);
var nextCell = $(this).next('td').length > 0 ? $(this).next('td') : null;
if (index%2==0&&nextCell && currentCell.text() !== nextCell.text()) {
currentCell.css('backgroundColor', 'yellow');
nextCell.css('backgroundColor', 'yellow');
}
});
});
<强> HTML 强>
<table id="coa_history_data">
<tr class="data-in-table">
<th>old Name</th>
<th>New Name</th>
<th>Old Phone</th>
<th>New Phone</th>
<th>Old Age</th>
<th>New Age</th>
</tr>
<tbody>
<tr class="data-in-table">
<td>Alphy</td>
<td>Alphy</td>
<td>015</td>//should be highlited
<td>016</td>//should be highlited
<td>23</td>//should be highlited
<td>24</td>//should be highlited</tr>
<tr class="data-in-table">
<td>Tom</td>
<td>Tom</td>
<td>12</td>
<td>12</td>
<td>65</td>//should be highlited
<td>30</td>//should be highlited</tr>
<tr class="data-in-table">
<td>will</td>
<td>will</td>
<td>45</td>
<td>45</td>
<td>20</td>
<td>20</td>
</tr>
</tbody>
</table>
答案 1 :(得分:0)
继承我的解决方案:
<table id="coa_history_data">
<tr>
<th>old Name</th>
<th>New Name</th>
<th>Old Phone</th>
<th>New Phone</th>
<th>Old Age</th>
<th>New Age</th>
</tr>
<tbody>
<tr class="data-in-table">
<td>Alphy</td>
<td>Alphy</td>
<td>015</td>
<td>016</td>
<td>23</td>
<td>24</td>
</tr>
<tr class="data-in-table">
<td>Tom</td>
<td>Tom</td>
<td>12</td>
<td>12</td>
<td>65</td>
<td>30</td>
</tr>
<tr class="data-in-table">
<td>will</td>
<td>will</td>
<td>45</td>
<td>45</td>
<td>20</td>
<td>20</td>
</tr>
</tbody>
</table>
的JScript:
$("#coa_history_data tbody tr.data-in-table").each(function() {
var $firstCell = $('td:eq(2)', this);
var $secondCell = $('td:eq(3)', this);
var $thirdCell = $('td:eq(4)', this);
var $fourthCell = $('td:eq(5)', this);
if($firstCell.text() != $secondCell.text()){
$firstCell.css('backgroundColor','yellow');
$secondCell.css('backgroundColor','yellow');
}
if($thirdCell.text() != $fourthCell.text()){
$thirdCell.css('backgroundColor','orange');
$fourthCell.css('backgroundColor','orange');
}
});
答案 2 :(得分:0)
好的,您想要突出显示我理解的特定字段<td>
。
更好的解决方案是在创建表时进行。
我确定此方法的效果更好,我在此处进行了测试 JsPerf
所以以这种方式创建你的表:
重要提示:当然您将拥有动态值,因此请将其置于 <td>
内的值变量中。
var content = [];
for (var i=0; i < len; i++){
if (i==0) //i dont know what is your condition to the tr be 'data-in-table' so change if needs
class = "data-in-table";
content.push("<tr class='"+class+"'> "+
"<td>will</td> "+
"<td>will</td> "+
"<td>"+value+"</td> "+
"<td>"+value+"</td> "+
"<td class=hlight>"+value+"</td>"+
"<td class=hlight>"+value+"</td>"+
"</tr>");
}
document.getElementById('your_table_wrapper').innerHTML = "<table id=coa_history_data>"+
"<tr> "+
"<th>old Name</th> "+
"<th>New Name</th> "+
"<th>Old Phone</th> "+
"<th>New Phone</th> "+
"<th>Old Age</th> "+
"<th>New Age</th> "+
"</tr> "+
"<tbody> "+
content.join('')+
"</tbody> "+
"</table>;
并使用此CSS:
.hlight {
background-color: yellow;
}