这是此question的扩展。
我们得到了类似于以下结构的结构,具体取决于类score
的元素的值,我们添加了一个类win
或lose
。
<div class="war">
<div class="team">
<span class="score">8</span>
</div>
<div class="team">
<span class="score">5</span>
</div>
</div>
<div class="war">
<div class="team">
<span class="score">12</span>
</div>
<div class="team">
<span class="score">25</span>
</div>
</div>
<div class="war">
<div class="team">
<span class="score">57</span>
</div>
<div class="team">
<span class="score">57</span>
</div>
</div>
用户Rory McCrossan提供了有效的解决方案https://stackoverflow.com/a/52757164/4236048
document.querySelectorAll('.war').forEach(function(war) {
var scores = Array.from(war.getElementsByClassName("score"));
var winIndex = scores.reduce((iMax, x, i, arr) => parseInt(x.innerText, 10) > parseInt(arr[iMax].innerText, 10) ? i : iMax, 0);
scores.forEach((score, i) => score.classList.add(winIndex == i ? 'win' : 'lose'));
});
但是我没有考虑平局的情况,其解决方案不再可行。
有没有想到在这种新情况下逻辑如何? .win
/ .lose
/ .draw
预先感谢
答案 0 :(得分:1)
更新#2
比较WebMvConfigurer
项内的得分并将类别应用于.war
项
.score
function applyScoreClass() {
$('.war').each(function(index, elem) {
var $elem = $(elem);
var warScores = [];
$elem.find('.score').each(function(i,el) {
var txt = $(el).text();
var num = (txt && txt.length > 0) ? parseInt(txt) : null;
if (num !== null) {
warScores.push({
elem: $(el),
score: num
});
}
});
if (warScores.length === 2) {
if (warScores[0].score > warScores[1].score) {
warScores[0].elem.addClass('win');
warScores[1].elem.addClass('lose');
} else if (warScores[0].score === warScores[1].score) {
warScores[0].elem.addClass('draw');
warScores[1].elem.addClass('draw');
} else if (warScores[0].score < warScores[1].score) {
warScores[1].elem.addClass('win');
warScores[0].elem.addClass('lose');
}
}
});
}
applyScoreClass();
.war {
border: 1px solid #d8d8d8;
}
.win {
color: green;
}
.draw {
color: orange;
}
.lose {
color: red;
}
答案 1 :(得分:0)
为HTML div / span定义ID值 内部循环得分数组声明
devtools.panels