gpt4 book ai didi

javascript - 使用 javascript 查找表行的最小值

转载 作者:行者123 更新时间:2023-12-02 17:37:47 25 4
gpt4 key购买 nike

好的,目前我正在使用此代码来查找它,但我需要过滤掉空字段,以便所有空白单元格不会变成红色。

$('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

<小时/>

查找整个表中的最小值

http://jsfiddle.net/6DgAW/

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');
<小时/>

找出每行的最小值

http://jsfiddle.net/DggUN/18/

$('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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com