0

http://jsfiddle.net/dweiliu/bcHsy/2/

我已经有一段时间了,我错过了一些东西......

jQuery的相关块。

$('article.weeklyStandings').each(function(){
    var winningPercentage = $('tr.record:nth-of-type(2)',this).find('td:nth-of-type(4)');
    $('tr.record',this).each(function(){
        var percentageWon = $(this).find('td:nth-of-type(4)');
        if (percentageWon == winningPercentage) {
            $('td:nth-of-type(1)', this).html("winner");
        }
    });
});

jsfiddle 链接中提供的上下文:http: //jsfiddle.net/dweiliu/bcHsy/2/

我有一个表,在第 4 列中有一些结果。我想遍历列中的每一行并将值与第二行进行比较。如果是 == 到那个值,我想做点什么。

我究竟做错了什么?

4

2 回答 2

0

尝试比较text()您已执行的两个 jquery 选择器的结果值。所以,改变这一行:

if (percentageWon == winningPercentage)

读书:

if (percentageWon.text() == winningPercentage.text())

希望有帮助。

于 2013-08-08T18:46:00.717 回答
0

试试这个,它更有效,因为它缓冲元素并且只需要一次迭代

$(document).ready(function() {
    var count = 0;
    var container = $('article.weeklyStandings'); // buffer the container
    var records = container.find('tr.record'); // buffer the winnin records
    var winner = 0;

    records.each(function() {
        var cells = $(this).children('td');
        // use text() and trim so extra space do not break the number conversion
        var gamesWon = parseInt(cells.eq(1).text().trim()); 
        var gamesPlayed = parseInt(cells.eq(2).text().trim());                  
        var percentage = (gamesWon / gamesPlayed * 100).toFixed(1);                 

        // save the percentage so it could be selected
        cells.eq(3).attr("wining-percent", percentage).text(percentage + "%");

        // add to the players count
        count++;
        if (percentage >= winner)
            winner = percentage; 
    });

    // write the amount of players
    container.find("h2").append(" - " + count + " Players");                
    // now add the winning class to the winners
    records.find('td[wining-percent="' + winner + '"]').parent().addClass("winner");
});

我建议处理信息以从服务器获取 JSON,而不是从 DOM 中提取值。

于 2013-08-08T19:04:29.763 回答