【发布时间】:2017-06-02 05:03:59
【问题描述】:
下面的表格是从 MySQL 加载到 HTML 中的:
我有这个脚本突出显示每列的两个最低和两个最高:
$(document).ready(function(){
var $table = $("#tbTodos");
$table.find("th").each(function(columnIndex)
{
var oldValue=0, currentValue=0;
var $trs = $table.find("tr");
var highElements = [];
var highElements2 = [];
var lowElements = [];
var lowElements2 = [];
var lowestValue = 999999;
var lowestValue2 = 999999;
var highestValue = 0;
var highestValue2 = 0;
$trs.each(function(index, element)
{
oldValue= currentValue;
var cell = $(this).find("td:eq("+ columnIndex +")");
if (cell.length!=0)
{
currentValue= parseInt(cell.html());
if(currentValue < lowestValue)
{
if(currentValue < lowestValue2)
{
lowestValue2 = lowestValue;
lowElements2 =lowElements.pop();
//lowElements2.push((cell));
}
lowestValue = currentValue;
// lowElements = [];
lowElements.push(cell);
}
else if (currentValue == lowestValue) {
lowElements.push(cell);
}
if (currentValue > highestValue)
{
highestValue2 = highestValue;
highElements2 = highElements.pop();
// highElements2.push(highElements.push(cell));
highestValue = currentValue;
// highElements = [];
highElements.push(cell);
}
else if (currentValue == highestValue) {
highElements.push(cell);
}
}
});
$.each(lowElements2, function(i, e){
$(e).addClass('highest2');
});
$.each(lowElements, function(i, e){
$(e).removeClass('highest2').addClass('highest');
});
$.each(highElements2, function(i, e){
$(e).addClass('lowest2');
});
$.each(highElements, function(i, e){
$(e).removeClass('lowest2').addClass('lowest');
});
});
});
css:
.highest{
background-color:#ff4040;
}
.highest2{
background-color:#f07878;
}
.lowest{
background-color:#66cc47;
}
.lowest2{
background-color:#aee59d ;
}
每列中的第一个最高和第一个最低标记是可以的,但在某些列中最高和最低的第二个值是错误的,例如 7 和 8;并且在第一列中没有第二高的数字。
【问题讨论】:
-
你能在jsfiddle.net上提供一个演示吗?
-
请包含相关的HTML、CSS;提供您代码的“minimal reproducible example”sn-p,以便我们可以轻松重现您的问题并提供解决方案。如果您让我们能够轻松地为您提供帮助,您可能会得到更好、更具体、更实用的问题答案。
标签: jquery css html-table cell highlight