简体   繁体   English

如何比较html表的两列值并用颜色突出显示不匹配的数据?

[英]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>

enter image description here在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个DIV,并使用javascript突出显示不匹配的内容 - Compare two DIVs and highlight the unmatched content using javascript 比较 html 表中的两个单元格,如果不同则突出显示行 - Compare two cells in a html table and highlight row if different 比较具有相同字段的两个数组 => 如果值不同,则在 html 表中突出显示 - Compare two array with same fields => if value is different highlight in html table 如何比较两个数组并删除不匹配的记录 - How to compare two arrays and remove unmatched record 如何过滤/搜索/突出显示 HTML 表格中的列? - How to filter/search/highlight columns in HTML table? 逐行比较两个html表数据并使用jQuery突出显示 - compare two html tables data line by line and highlight using jquery 比较两行(HTML表)数据(即行[i]与行[i + 1]),并使用Jquery / Javascript突出显示更改 - Compare two rows (HTML Table) data i.e..row[i] with row[i+1] and highlight changes using Jquery/Javascript 比较数组并删除不匹配的值 - Compare array and remove unmatched values html表如何通过更改悬停边框来突出显示列? - How can a html table highlight columns by changing the border on hover? 如果从JSON文件(HTML)检索表数据,则两个不同表列的值的乘法将不起作用 - Multiplication of values from two different table columns does not work if table data is retrieved from a JSON file (HTML)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM