Jquery - 多次将类应用到最高值
我有以下表结构,其结果来自两个单独的 php 查询。
<tr>
<td>Item 1</td>
<td>£<?=$row1['item_1']?></td>
<td>£<?=$row2['item_1']?></td>
</tr>
<tr>
<td>Item 2</td>
<td>£<?=$row1['item_2']?></td>
<td>£<?=$row2['item_2']?></td>
</tr>
<tr>
<td>Item 3</td>
<td>£<?=$row1['item_3']?></td>
<td>£<?=$row2['item_3']?></td>
</tr>
我想做的是在每一行中突出显示从数据库中提取的最高数字。
任何帮助表示赞赏!
更新
我已将以下代码添加到页面中,但我一生都无法让它工作。我可以看到它在 jsFiddle 上运行,但我一定做错了什么。请帮忙
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js "></script>
<script type="text/javascript">
jQuery(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('red');
});
</script>
I have the following table structure which results come from two seperate php queries.
<tr>
<td>Item 1</td>
<td>£<?=$row1['item_1']?></td>
<td>£<?=$row2['item_1']?></td>
</tr>
<tr>
<td>Item 2</td>
<td>£<?=$row1['item_2']?></td>
<td>£<?=$row2['item_2']?></td>
</tr>
<tr>
<td>Item 3</td>
<td>£<?=$row1['item_3']?></td>
<td>£<?=$row2['item_3']?></td>
</tr>
What I'm trying to do is within each row highlight the highest number pulled from the database.
Any help is appreciated!
UPDATE
I've added the following code to the page but can't for the life of me get it working. I can see it working on jsFiddle but I must be doing something wrong. Help please
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js "></script>
<script type="text/javascript">
jQuery(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('red');
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有格式为
x.xx
:更新: 的数字,我可能会更好地为此创建一个插件。类似于:
然后可以用作:
DEMO
Old答案:
DEMO
Assuming you have numbers of the format
x.xx
:Update: I might be even better to create a plugin for that. Something like:
Which can then be used as:
DEMO
Old answer:
DEMO
你可以这样做(但我认为可以做得更好):
在这里小提琴: http://jsfiddle.net/nicolapeluchetti /DYUaP/
You could do (but could be done much better i think):
fiddle here: http://jsfiddle.net/nicolapeluchetti/DYUaP/