好的,目前我正在使用此代码来查找它,但我需要过滤掉空字段,以便所有空白单元格不会变成红色。
$('tr').each(function highlight() {
var $td = $(this).children('td');
// find all the values
var vals = $td.map(function () {
return +$(this).text();
}).filter(function (val) {
return val !== null
});
}).get();
// then find their minimum
var min = Math.min.apply(Math, vals);
// tag any cell matching the min value
$td.filter(function highlight() {
return +$(this).text() === min;
}).addClass('alert alert-danger');
});
那么如何过滤掉空值呢?
最佳答案
如果 $('#asd').text()
计算结果为“”
然后 +$('#asd').text()
将计算为 0
查找整个表中的最小值
var vals = $('tr td').map(function () {
return isNaN(parseInt($(this).text(), 10)) ? parseInt($(this).text(), 10) : null;
}).get();
// then find their minimum
var min = Math.min.apply(Math, vals);
// tag any cell matching the min value
$('tr td').filter(function () {
return parseInt($(this).text(), 10) === min;
}).addClass('alert alert-danger');
<小时/>
找出每行的最小值
$('tr').each(function(){
var vals = $('td,th',this).map(function () {
return parseInt($(this).text(), 10) ? parseInt($(this).text(), 10) : null;
}).get();
// then find their minimum
var min = Math.min.apply(Math, vals);
// tag any cell matching the min value
$('td,th', this).filter(function () {
return parseInt($(this).text(), 10) === min;
}).addClass('alert alert-danger');
});
关于javascript - 使用 javascript 查找表行的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22470002/