Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery-多次将类应用于最高值_Jquery_Highlight - Fatal编程技术网

Jquery-多次将类应用于最高值

Jquery-多次将类应用于最高值,jquery,highlight,Jquery,Highlight,我有下面的表结构,它的结果来自两个不同的php查询 <tr> <td>Item 1</td> <td>&pound;<?=$row1['item_1']?></td> <td>&pound;<?=$row2['item_1']?></td> </tr> <tr> <td>Item 2</td> <td>&p

我有下面的表结构,它的结果来自两个不同的php查询

<tr>
<td>Item 1</td>
<td>&pound;<?=$row1['item_1']?></td>
<td>&pound;<?=$row2['item_1']?></td>
</tr>
<tr>
<td>Item 2</td>
<td>&pound;<?=$row1['item_2']?></td>
<td>&pound;<?=$row2['item_2']?></td>
</tr>
<tr>
<td>Item 3</td>
<td>&pound;<?=$row1['item_3']?></td>
<td>&pound;<?=$row2['item_3']?></td>
</tr>

项目1
&磅;
&磅;
项目2
&磅;
&磅;
项目3
&磅;
&磅;
我要做的是在每一行中突出显示从数据库中提取的最大数字

感谢您的帮助

更新

我已经在页面中添加了以下代码,但我一生都无法让它正常工作。我可以看到它在JSFIDLE上工作,但我一定是做错了什么。请帮忙

<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>

jQuery(函数($){
$.fn.max=函数(回调){
var max=null,
maxIndex=null;
这个。每个(函数(){
var value=callback.call(this);
如果(+值===值){
如果(!max | | value>max){
最大值=最大值;
maxIndex=$(this.index();
}
}
});
返回max!==null?this.eq(maxIndex):$();
};
}(jQuery));
$('tr')。每个(函数(){
$(this).children('td').max(function(){
var value=+$(this.text().substr(1);
如果(!isNaN(值)){
返回值;
}
}).addClass(“红色”);
});

假设您有格式为
x.xx
的数字:

更新:我甚至可以为它创建一个插件。比如:

(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('highlight');
});


旧答案:

$('tr').each(function() {
    var $tds = $(this).children('td'),
        max = null,
        maxIndex = null;

    $tds.each(function() {
        var value = +$(this).text().substr(1);
        if(!isNaN(value)) {
            if(!max || value > max) {
                max = value;
                maxIndex = $(this).index();
            }
        }
    });
    if(maxIndex !== null) {
        $tds.eq(maxIndex).addClass('highlight');
    }
});
你可以做(但我认为可以做得更好):

Array.max=函数(数组){
返回Math.max.apply(数学,数组);
};
Array.keyPosition=函数(数组,值){
对于(i=0;i

在这里摆弄:

请发布生成的HTML。我喜欢插件的想法。我分叉并制作了一些mod:一个用来接受回调来解析内容,另一个用来执行回调,最后,它返回原来的chaineability集合。为此干杯,演示正是我想要的。我已经在我的页面上尝试过了,但无法让它工作。我需要Jquery的特定版本吗?结果是从数据库中提取的,而不是html代码中的静态结果,这有关系吗?@LinusIT:您至少需要jQuery 1.4。数据来自哪里并不重要。重要的是生成的HTML。@Felix King:HTML输出:
code
DBW£;85英镑;54千兆瓦&磅;2英镑;897
Array.max = function(array) {
    return Math.max.apply(Math, array);
};

Array.keyPosition = function(array, value) {
    for (i = 0; i < array.length; i++) {
        if (array[i] === value) {
            return i;
        }
    }
    return false;
}


$('table tr').each(function() {
    var numbers = [];
    $(this).children('td').each(function(i, el) {
        var number = parseInt($(this).text(), 10);
        if (isNaN(number)){
            number = -1;
        }
        numbers.push(number);
    });
    var max = Array.max(numbers);
    var index = Array.keyPosition(numbers, max);
    $(this).find('td:eq('+index+')').css('background-color', 'yellow');

});