Javascript每个tr中的最大数字,跳过3列
我有一个表,它将为每个tr中的最大数量添加一个类 我希望它跳过前3列,而不是搜索它们。如果有最高的倍数,那么也要加粗 我会在这里粘贴代码,也会在这里摆弄 HTMLJavascript每个tr中的最大数字,跳过3列,javascript,jquery,Javascript,Jquery,我有一个表,它将为每个tr中的最大数量添加一个类 我希望它跳过前3列,而不是搜索它们。如果有最高的倍数,那么也要加粗 我会在这里粘贴代码,也会在这里摆弄 HTML <style> .highest { font-weight: bold; } </style> <table width="300"> <tr> <th>no</th>
<style>
.highest {
font-weight: bold;
}
</style>
<table width="300">
<tr>
<th>no</th>
<th>no</th>
<th>no</th>
<th>yes</th>
<th>yes</th>
<th>yes</th>
</tr>
<tr>
<td>150</td>
<td>name</td>
<td>10.5</td>
<td>1.5</td>
<td>12</td>
<td>9.3</td>
</tr>
<tr>
<td>12.0</td>
<td>name</td>
<td>150</td>
<td>150</td>
<td>13.5</td>
<td>150</td>
</tr>
<tr>
<td>160</td>
<td>name</td>
<td>115</td>
<td>15</td>
<td>11</td>
<td>160</td>
</tr>
<tr>
<td>145</td>
<td>name</td>
<td>151</td>
<td>12</td>
<td>18</td>
<td>18</td>
</tr>
</table>
小提琴
要跳过前3列,请使用:
$(this).children('td').filter(function(i) {
return i > 2;
}).max(...)
filter函数接收集合中元素的从零开始的位置
如果要突出显示具有最大值的多个条目,
maxIndex
需要是一个数组,而不是单个值。当值===max
时,将当前索引推送到数组上。或者将其设置为元素的jQuery集合,而不是索引,并将当前元素添加到其中。要跳过前3列,请使用:
$(this).children('td').filter(function(i) {
return i > 2;
}).max(...)
$('tr').each(function(){
var this = $(this);
var max = -Infinity;
var indexes = [];
this.find('td:gt(2)').each(function(index){
var this_num = ($(this).text() >> 0);
if ( max < this_num ) {
max = this_num;
indexes = [index];
} else if (max == this_num ) {
indexes.push(this_num);
}
});
$(indexes).each(function(index){
this.find('td:eq('+index+')').addClass('highest');
});
});
filter函数接收集合中元素的从零开始的位置
如果要突出显示具有最大值的多个条目,maxIndex
需要是一个数组,而不是单个值。当值===max
时,将当前索引推送到数组上。或者将其设置为元素的jQuery集合,而不是索引,并将当前元素添加到其中。$('tr')。每个(函数(){
$('tr').each(function(){
var this = $(this);
var max = -Infinity;
var indexes = [];
this.find('td:gt(2)').each(function(index){
var this_num = ($(this).text() >> 0);
if ( max < this_num ) {
max = this_num;
indexes = [index];
} else if (max == this_num ) {
indexes.push(this_num);
}
});
$(indexes).each(function(index){
this.find('td:eq('+index+')').addClass('highest');
});
});
var this=$(this);
var max=-无穷大;
var指数=[];
this.find('td:gt(2)')。每个(函数(索引){
var this_num=($(this).text()>>0);
如果(最大值<此数值){
max=这个数值;
索引=[索引];
}else if(max==此数值){
index.push(这个数值);
}
});
$(索引)。每个(函数(索引){
this.find('td:eq('+index+')).addClass('highest');
});
});
应该有用。。尚未测试:|$('tr')。每个(函数(){
var this=$(this);
var max=-无穷大;
var指数=[];
this.find('td:gt(2)')。每个(函数(索引){
var this_num=($(this).text()>>0);
如果(最大值<此数值){
max=这个数值;
索引=[索引];
}else if(max==此数值){
index.push(这个数值);
}
});
$(索引)。每个(函数(索引){
this.find('td:eq('+index+')).addClass('highest');
});
});
应该有用。。尚未测试:|只需将选择器作为参数添加到插件中,并根据该参数进行筛选:
jQuery(function($) {
$.fn.max = function(selector) {
var elems = $();
this.each(function() {
var max = 0,
ele = $(this).find(selector).each(function(i,el) {
var n = parseFloat($(el).text());
if ( n > max ) max = n;
}).filter(function() {
return parseFloat($(this).text()) === max;
});
elems = elems.add(ele);
});
return elems;
};
}(jQuery));
$('tr').max('td:gt(2)').addClass('highest');
只需在插件中添加一个选择器作为参数,然后根据该参数进行过滤:
jQuery(function($) {
$.fn.max = function(selector) {
var elems = $();
this.each(function() {
var max = 0,
ele = $(this).find(selector).each(function(i,el) {
var n = parseFloat($(el).text());
if ( n > max ) max = n;
}).filter(function() {
return parseFloat($(this).text()) === max;
});
elems = elems.add(ele);
});
return elems;
};
}(jQuery));
$('tr').max('td:gt(2)').addClass('highest');
我宁愿使用
parseInt($(this.text())
而不是+$(this.text())
谢谢,我会更改它。我宁愿使用parseInt($(this.text())
而不是+$(this.text())
谢谢,我会更改它。我将$(this.children.children('td').max(function(){/code>)更改为$(this.children.children('td')).filter(function(i){return i>2;}).max(function(){
现在不加粗任何内容。很抱歉,Javascript的新问题是这个问题。eq(maxIndex)
与父节点的所有子节点相关,但是这个.eq()
只是相对于集合(不包括前3列)计数。这就是为什么您应该保存元素而不是索引。我将$(This).children('td').max(function(){
更改为$(This).children('td').filter(function(I){return I>2;}).max(function(){
并且现在不加粗。很抱歉,JavaScription的新问题是这个.eq(maxIndex)
maxIndex
是相对于父节点的所有子节点的,但是这个.eq()
只是相对于集合计数,它不包括前3列。这就是为什么您应该保存元素而不是索引。