Apply class to highest value over seperate columns
I have some code that works already but I'd like to change my page design, in doing this the add class script doesn't work. Here's what I've got for the design:
<tr>
<td>Item 1</td>
<td>£<?php echo $row1['item1']; ?></td>
<td>£<?php echo $row2['item1']; ?></td>
<td>£<?php echo $row3['item1']; ?></td>
<td>£<?php echo $row4['item1']; ?></td>
<td>Item 2</td>
<t开发者_开发问答d>£<?php echo $row1['item2']; ?></td>
<td>£<?php echo $row2['item2']; ?></td>
<td>£<?php echo $row3['item2']; ?></td>
<td>£<?php echo $row4['item2']; ?></td>
<td>Item 3</td>
<td>£<?php echo $row1['item3']; ?></td>
<td>£<?php echo $row2['item3']; ?></td>
<td>£<?php echo $row3['item3']; ?></td>
<td>£<?php echo $row4['item3']; ?></td>
</tr>
Here's the jQuery code I've got:
<script type='text/javascript'>
//<![CDATA[
$(window).load(function(){
(function($) {
$.fn.max = function(callback) {
var max = null,
maxIndex = null;
this.each(function() {
var value = callback.call(this);
if (+value === value) {
if (!max || value > max) {
max = value;
maxIndex = $(this).index();
}
}
});
return max !== null ? this.eq(maxIndex) : $();
};
}(jQuery));
$('tr').each(function() {
$(this).children('td').max(function() {
var value = +$(this).text().substr(1);
if (!isNaN(value)) {
return value;
}
}).addClass('green bold');
});
});
//]]>
</script>
This works but only for each tr, meaning it highlights the highest value in each row, where as I need to it to highlight the highest value through each group (item 1, 2 and 3).
Can the code be changed for this to work?
Try looping through the TDs without first looping through the TRs:
$('table').find('td').max(function() {
var value = +$(this).text().substr(1);
if (!isNaN(value)) {
return value;
}
}).addClass('green bold');
精彩评论