作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个 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');
}
});
我想要一些建议,如何处理?
最佳答案
为了独立于列数,我建议如下所示。
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>
关于javascript - 突出显示具有不同值的表列 td,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20349821/
有没有一种方法可以“标记”对象的属性,使它们在反射中“突出”? 例如: class A { int aa, b; string s1, s2; public int AA
我是一名优秀的程序员,十分优秀!