gpt4 book ai didi

javascript - 突出显示具有不同值的表列 td

转载 作者:数据小太阳 更新时间:2023-10-29 05:30:00 25 4
gpt4 key购买 nike

我有一个 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');
}
});

我想要一些建议,如何处理?

最佳答案

为了独立于列数,我建议如下所示。

http://jsfiddle.net/hA5G8/

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com