我正在尝试向HTML表添加一项功能,该功能突出显示与其他值相比所有不同的值。比较是逐行进行的。
经过努力,我设法实现了以下JQuery / Javascrit代码。我很确定这是不一种高效/优雅/快速的方法,但这是我解决问题的唯一方法。
HTML表格非常庞大且复杂,因此很难在此处发布。
问题我遇到的问题是脚本在循环中工作正常,但如果我把它放在FOR - LOOP中它就会挂起来我坚持下去明白为什么。
var numRows = $('.ctable tbody tr').length, numCols = $('.ctable tbody tr:first th').length, v, undefined;
var values = new Array(numRows);
var noDuplicates = new Array(numCols);
var result = new Array(numCols);
for (i = 1; i = numRows; i++) {
// Get a row and copy into an array the values of each VISIBLE cell
$(".ctable tbody tr:eq(" + i + ") td.values:visible").each(function(){
v = $(this).text();
values.push(v.trim());
});
// Remove from the array the 'undefined' values
values = values.filter(function(item){
return item !== undefined;
});
// Push into new array duplicate values
noDuplicates = return_duplicates(values);
// Compare the two arrays and get the differences (uses underscore.js)
result = _.difference(values, noDuplicates);
// This is a 'highlight' plugin and you may pass to it an array
$(".ctable tbody tr:eq(" + i + ") td.values:visible").highlight(values);
}
function return_duplicates(arr) {
var len=arr.length, out=[], counts={};
for (var i=0;i<len;i++) {
var item = arr[i];
counts[item] = counts[item] >= 1 ? counts[item] + 1 : 1;
}
for (var item in counts) {
if(counts[item] > 1)
out.push(item);
}
return out;
}
答案 0 :(得分:1)
尝试
for (i = 1; i < numRows; i++) {
而不是
for (i = 1; i = numRows; i++) {