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列中有一个包含一些结果的表。我想遍历列中的每一行并将值与第二行进行比较。如果= =那个值,我想做点什么。
我做错了什么?
答案 0 :(得分:0)
尝试比较已执行的两个jquery选择器的结果的text()
值。所以,改变这一行:
if (percentageWon == winningPercentage)
阅读:
if (percentageWon.text() == winningPercentage.text())
希望有所帮助。
答案 1 :(得分: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中选择值。