使用jQuery或JavaScript比较表字符串
Table string compare using jQuery or JavaScript
如何使用原生JavaScript或jQuery比较来自两个表的数据?例如,我在同一个HTML页面中有两个表,我想比较这些表中的一些列。例如:列"username"在两个表中都包含一些id,我想突出显示那些"username"列中"first name"answers"last name"列单元格相同的"username"单元格中不具有相同id的"username"单元格。
这里有一个小提琴
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<table class="table">
<caption>Table One</caption>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>7872</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>9890</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>7719</td>
</tr>
</tbody>
</table>
</div>
<div class="container">
<table class="table">
<caption>Table Two</caption>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>7872</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>2232</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>7719</td>
</tr>
</tbody>
</table>
</div>
您需要查看每个表格并检查单元格是否不同
$(".table:first tbody tr").each(function(){
var tabletr=$(this);
var tableone=$(this).children("td");
$(".table:last tbody tr").each(function(){
if(tableone.eq(0).text()== $(this).children("td").eq(0).text() && tableone.eq(1).text()== $(this).children("td").eq(1).text() && tableone.eq(2).text()!= $(this).children("td").eq(2).text()){
tabletr.css("background","#f00");
$(this).css("background","#f00");
console.log("USERNAME DIFFERENT!"+$(this).children("td").eq(2).text());
}
});
});
https://jsfiddle.net/DTcHh/11529/相关文章:
- 反向字符串比较
- ExpressJS:奇怪的字符串比较
- 宽松的字符串比较
- 在angularJS中用字符串比较检查Json对象
- 在JavaScript中使用===进行字符串比较有意义吗
- 字符串比较显示从flash[:alert]检索到的字符串上的错误
- JSLint字符串比较错误应为'==='而是看到'=='
- 字符串比较在d3.rgb中不起作用
- 为什么这个字符串比较在Jest的期望中没有失败
- jQuery this.href 字符串比较不起作用
- 字符串比较不起作用
- 字符串比较与预期不符
- Javascript中的字符串比较不起作用
- 正则表达式(字符串比较)
- j查询字符串比较变量
- 字符串比较 JavaScript 中的失败
- 当字符串中存在空格时,字符串比较会给出错误的输出
- Ant JavaScript 相同的字符串比较给出了错误的结果,但 char compare 给出了字符串中每个字符的
- 正则表达式字符串比较忽略符号
- JavaScript 中的序号字符串比较