[英]how to compare html table two columns values and highlight the unmatched data with color?
This is my html table.这是我的 html 表。 I want to highlight the unmatched data with color.
我想用颜色突出显示不匹配的数据。 I have written javascript for that but it works only for the first row.
我为此编写了 javascript,但它仅适用于第一行。 It's not working for the remaining rows.
它不适用于剩余的行。 Please help me about this logic.
请帮我解决这个逻辑。
sample output Image:示例输出图像:
highlight($("#new"), $("#old")); function highlight(newElem, oldElem) { var newText = newElem.text(); var oldText = oldElem.text(); var text = ""; newElem.text().split("").forEach(function(value, index) { if (value != oldText.charAt(index)) text += "<span class='highlight'>" + value + "</span>"; else text += value; }); newElem.html(text); }
.highlight { color: red; } table, th, td { border: 1px solid black; border-collapse: collapse; } th, td { padding: 5px; text-align: left; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tdata"> <tr class="tvalue"> <th>S.No</th> <th>VALUE1</th> <th>VALUE2</th> </tr> <tr class="tvalue"> <td>1</td> <td id="old">this is veerendar</td> <td id="new">this is vearander</td> </tr> <tr class="tvalue"> <td>2</td> <td id="old">123456789</td> <td id="new">124353789</td> </tr> <tr class="tvalue"> <td>3</td> <td id="old">12 34 56 78 32 45</td> <td id="new">34 23 56 79 32 46</td> </tr> <tr class="tvalue"> <td>4</td> <td id="old">ab cd ef sd ef wd</td> <td id="new">bc er ef sd ef we</td> </tr> </table>
Thanks for sharing this code snippet.感谢您分享此代码片段。 It really helped me.
它真的帮助了我。 I tried this and it worked for me.
我试过这个,它对我有用。 The changes I had to make were add a number at the end of id or class whatever you are using.
我必须做的更改是在 id 或 class 的末尾添加一个数字,无论您使用什么。 Since my page was generated from a script I didn't have any problem adding a number at the end.
由于我的页面是从脚本生成的,因此在最后添加数字没有任何问题。
$( ".tvalue" ).each(function( i ) { highlight($("#new"+i), $("#old"+i)); }); function highlight(newElem, oldElem) { var newText = newElem.text(); var oldText = oldElem.text(); var text = ""; newElem.text().split("").forEach(function(value, index) { if (value != oldText.charAt(index)) text += "<span class='highlight'>" + value + "</span>"; else text += value; }); newElem.html(text); }
.highlight { color: red; } table, th, td { border: 1px solid black; border-collapse: collapse; } th, td { padding: 5px; text-align: left; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tdata"> <tr class="tvalue"> <th>S.No</th> <th>VALUE1</th> <th>VALUE2</th> </tr> <tr class="tvalue"> <td>1</td> <td id="old1">this is veerendar</td> <td id="new1">this is vearander</td> </tr> <tr class="tvalue"> <td>2</td> <td id="old2">123456789</td> <td id="new2">124353789</td> </tr> <tr class="tvalue"> <td>3</td> <td id="old3">12 34 56 78 32 45</td> <td id="new3">34 23 56 79 32 46</td> </tr> <tr class="tvalue"> <td>4</td> <td id="old4">ab cd ef sd ef wd</td> <td id="new4">bc er ef sd ef we</td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.