如果前 4 children 满足要求,则显示特定文本,Jquery
Display specific texts if the first 4 children meet the requirements, with Jquery
我有一个包含多行和多列的 table。我想要的是:如果 5 个 children 中的前 4 个具有特定的 class,则将特定文本附加到另一个 div。否则,不是。我被这段代码困住了。任何帮助将不胜感激:
$('table > tbody tr').each(function() {
var counter = 0;
var ccc;
$('td.form a:nth-child(1), td.form a:nth-child(2), td.form a:nth-child(3),td.form a:nth-child(4)').each(function() {
if(( $(this).hasClass("form-win") ) || ( $(this).hasClass("form-loss") )) {
$(this).css('background','red');
counter++;
ccc = $(this).closest('tr.team_rank').find('td.team a').text();
$('results').append(ccc);
}
else {
$(this).css('background','blue');
}
});
});
我也更新了我的笔,以可视化 table:
https://codepen.io/tudor-taranu/pen/gZrmKR
$('table > tbody tr').each(function() {
var counter = 0;
var ccc;
$('td.form a:nth-child(1), td.form a:nth-child(2), td.form a:nth-child(3),td.form a:nth-child(4)').each(function() {
if(( $(this).hasClass("form-win") ) || ( $(this).hasClass("form-loss") )) {
$(this).css('background','red');
counter++;
ccc = $(this).closest('tr.team_rank').find('td.team a').text();
$('results').append(ccc);
}
else {
$(this).css('background','blue');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="leaguetable sortable table detailed-table" id="page_competition_1_block_competition_tables_7_block_competition_league_table_1_table" data-round_id="48876">
<thead>
<tr class="sub-head">
<th title="Loc" class="sortasc sortdefaultasc">#
</th>
<th class="text team sortdefaultasc">team
</th>
<th class="form nosort">last 5
</th>
</tr>
</thead>
<tbody>
<tr class="odd team_rank" id="team_rank_row_1824" data-team_id="1824">
<td class="rank rank-light-green">1
</td>
<td class="text team large-link">
<a title="CFR Cluj">CFR Cluj
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="CFR Cluj - Gaz Metan Mediaş 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win" title="Dinamo Bucureşti - CFR Cluj 0 - 3" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="CFR Cluj - Voluntari 5 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Concordia Chiajna - CFR Cluj 0 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="CFR Cluj - Dunărea Călăraşi 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_25281" data-team_id="25281">
<td class="rank rank-light-green">3
</td>
<td class="text team large-link">
<a title="Universitatea Craiova">Universitatea …
</a>
</td>
<td class="form">
<a class="form-icon form-win" title="Universitatea Craiova - Voluntari 3 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Concordia Chiajna - Universitatea Craiova 1 - 3" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Universitatea Craiova - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-loss" title="Botoşani - Universitatea Craiova 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw last" title="Universitatea Craiova - Sepsi 1 - 1">D
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_13720" data-team_id="13720">
<td class="rank rank-light-green">4
</td>
<td class="text team large-link">
<a title="Viitorul">Viitorul
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="FCSB - Viitorul 2 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Gaz Metan Mediaş - Viitorul 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win" title="Viitorul - Dinamo Bucureşti 4 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Voluntari - Viitorul 1 - 2" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="Viitorul - Concordia Chiajna 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_1827" data-team_id="1827">
<td class="rank rank-light-green">5
</td>
<td class="text team large-link">
<a title="Gaz Metan Mediaş">Gaz Metan Mediaş
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="CFR Cluj - Gaz Metan Mediaş 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-draw" title="Gaz Metan Mediaş - Viitorul 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="FCSB - Gaz Metan Mediaş 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Dinamo Bucureşti - Gaz Metan Mediaş 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win last" title="Gaz Metan Mediaş - Voluntari 2 - 0">W
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_37490" data-team_id="37490">
<td class="rank rank-orange">10
</td>
<td class="text team large-link">
<a title="Hermannstadt">Hermannstadt
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="Hermannstadt - Dinamo Bucureşti 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="Voluntari - Hermannstadt 2 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-win" title="Hermannstadt - Concordia Chiajna 2 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Dunărea Călăraşi - Hermannstadt 0 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="Hermannstadt - Botoşani 1 - 1">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_1775" data-team_id="1775">
<td class="rank rank-orange">11
</td>
<td class="text team large-link">
<a title="Dinamo Bucureşti">Dinamo Bucureşti
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="Hermannstadt - Dinamo Bucureşti 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="Dinamo Bucureşti - CFR Cluj 0 - 3" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Viitorul - Dinamo Bucureşti 4 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Dinamo Bucureşti - Gaz Metan Mediaş 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-draw last" title="Dinamo Bucureşti - FCSB 1 - 1">D
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_9221" data-team_id="9221">
<td class="rank rank-orange">12
</td>
<td class="text team large-link">
<a title="Dunărea Călăraşi">Dunărea Călăraşi
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Politehnica Iași - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Dunărea Călăraşi - Astra 1 - 2" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Universitatea Craiova - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Dunărea Călăraşi - Hermannstadt 0 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw last" title="CFR Cluj - Dunărea Călăraşi 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_9088" data-team_id="9088">
<td class="rank rank-orange">13
</td>
<td class="text team large-link">
<a title="Concordia Chiajna">Concordia Chiajna
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Concordia Chiajna - Politehnica Iași 3 - 6" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Astra - Concordia Chiajna 3 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Concordia Chiajna - Universitatea Craiova 1 - 3" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Hermannstadt - Concordia Chiajna 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss last" title="Concordia Chiajna - CFR Cluj 0 - 1">L
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_11607" data-team_id="11607">
<td class="rank rank-orange">14
</td>
<td class="text team large-link">
<a title="Voluntari">Voluntari
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Universitatea Craiova - Voluntari 3 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-win" title="Voluntari - Hermannstadt 2 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-loss" title="CFR Cluj - Voluntari 5 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Voluntari - Viitorul 1 - 2" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss last" title="Gaz Metan Mediaş - Voluntari 2 - 0">L
</a>
</td>
</tr>
</tbody>
</table>
<br>
<div class="results">RESULT:
</div>
这是我对你的问题的解决方案:在每个 tr
中,你必须检查每个 form-icon
是否有 class form-win
或 form-loss
(你的红色的),如果有的话加+1来反击。如果计数器达到 4,您知道前 4 个 form-icon
是红色的,您可以将该团队添加到 results
div.
这是脚本,所有注释都是为了更好地理解(我尝试使用您已经编写的代码):
$('table > tbody tr').each(function () {
var counter = 0; /* reset counter to every tr loop */
var ccc = $(this).closest('tr.team_rank').find('td.team a').text(); /* put in ccc the name of team every tr */
$(".form-icon", $(this)).each(function () {
if ($(this).hasClass("form-win") || $(this).hasClass("form-loss")) {
counter++ /*if a form-icon has the class form-win or form-loss (the red ones) add +1 to counter */
} else {
return false; /* if those classes there aren't exit from loop */
}
if (counter == 4) {
$('.results').append(ccc + ", "); /*if the counter is equals to 4 add the team to .result div */
}
});
});
这是正在运行的脚本。
$('table > tbody tr').each(function () {
var counter = 0; /* reset counter to every tr loop */
var ccc = $(this).closest('tr.team_rank').find('td.team a').text(); /* put in ccc the name of team every tr */
$(".form-icon", $(this)).each(function () {
if ($(this).hasClass("form-win") || $(this).hasClass("form-loss")) {
counter++ /*if a form-icon has the class form-win or form-loss (the red ones) add +1 to counter */
} else {
return false; /* if those classes there aren't exit from loop */
}
if (counter == 4) {
$('.results').append(ccc + ", "); /*if the counter is equals to 4 add the team to .result div */
}
});
});
/* This script removes last ', ' from your teams's string. You can remove it, if you don't need it */
$('.results').text(function (_, txt) {
return txt.slice(0, -2);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="leaguetable sortable table detailed-table" id="page_competition_1_block_competition_tables_7_block_competition_league_table_1_table" data-round_id="48876">
<thead>
<tr class="sub-head">
<th title="Loc" class="sortasc sortdefaultasc">#
</th>
<th class="text team sortdefaultasc">team
</th>
<th class="form nosort">last 5
</th>
</tr>
</thead>
<tbody>
<tr class="odd team_rank" id="team_rank_row_1824" data-team_id="1824">
<td class="rank rank-light-green">1
</td>
<td class="text team large-link">
<a title="CFR Cluj">CFR Cluj
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="CFR Cluj - Gaz Metan Mediaş 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win" title="Dinamo Bucureşti - CFR Cluj 0 - 3" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="CFR Cluj - Voluntari 5 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Concordia Chiajna - CFR Cluj 0 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="CFR Cluj - Dunărea Călăraşi 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_25281" data-team_id="25281">
<td class="rank rank-light-green">3
</td>
<td class="text team large-link">
<a title="Universitatea Craiova">Universitatea …
</a>
</td>
<td class="form">
<a class="form-icon form-win" title="Universitatea Craiova - Voluntari 3 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Concordia Chiajna - Universitatea Craiova 1 - 3" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Universitatea Craiova - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-loss" title="Botoşani - Universitatea Craiova 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw last" title="Universitatea Craiova - Sepsi 1 - 1">D
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_13720" data-team_id="13720">
<td class="rank rank-light-green">4
</td>
<td class="text team large-link">
<a title="Viitorul">Viitorul
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="FCSB - Viitorul 2 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Gaz Metan Mediaş - Viitorul 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win" title="Viitorul - Dinamo Bucureşti 4 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Voluntari - Viitorul 1 - 2" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="Viitorul - Concordia Chiajna 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_1827" data-team_id="1827">
<td class="rank rank-light-green">5
</td>
<td class="text team large-link">
<a title="Gaz Metan Mediaş">Gaz Metan Mediaş
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="CFR Cluj - Gaz Metan Mediaş 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-draw" title="Gaz Metan Mediaş - Viitorul 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="FCSB - Gaz Metan Mediaş 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Dinamo Bucureşti - Gaz Metan Mediaş 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win last" title="Gaz Metan Mediaş - Voluntari 2 - 0">W
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_37490" data-team_id="37490">
<td class="rank rank-orange">10
</td>
<td class="text team large-link">
<a title="Hermannstadt">Hermannstadt
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="Hermannstadt - Dinamo Bucureşti 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="Voluntari - Hermannstadt 2 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-win" title="Hermannstadt - Concordia Chiajna 2 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Dunărea Călăraşi - Hermannstadt 0 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="Hermannstadt - Botoşani 1 - 1">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_1775" data-team_id="1775">
<td class="rank rank-orange">11
</td>
<td class="text team large-link">
<a title="Dinamo Bucureşti">Dinamo Bucureşti
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="Hermannstadt - Dinamo Bucureşti 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="Dinamo Bucureşti - CFR Cluj 0 - 3" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Viitorul - Dinamo Bucureşti 4 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Dinamo Bucureşti - Gaz Metan Mediaş 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-draw last" title="Dinamo Bucureşti - FCSB 1 - 1">D
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_9221" data-team_id="9221">
<td class="rank rank-orange">12
</td>
<td class="text team large-link">
<a title="Dunărea Călăraşi">Dunărea Călăraşi
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Politehnica Iași - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Dunărea Călăraşi - Astra 1 - 2" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Universitatea Craiova - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Dunărea Călăraşi - Hermannstadt 0 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw last" title="CFR Cluj - Dunărea Călăraşi 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_9088" data-team_id="9088">
<td class="rank rank-orange">13
</td>
<td class="text team large-link">
<a title="Concordia Chiajna">Concordia Chiajna
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Concordia Chiajna - Politehnica Iași 3 - 6" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Astra - Concordia Chiajna 3 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Concordia Chiajna - Universitatea Craiova 1 - 3" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Hermannstadt - Concordia Chiajna 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss last" title="Concordia Chiajna - CFR Cluj 0 - 1">L
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_11607" data-team_id="11607">
<td class="rank rank-orange">14</td>
<td class="text team large-link">
<a title="Voluntari">Voluntari
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Universitatea Craiova - Voluntari 3 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-win" title="Voluntari - Hermannstadt 2 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-loss" title="CFR Cluj - Voluntari 5 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Voluntari - Viitorul 1 - 2" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss last" title="Gaz Metan Mediaş - Voluntari 2 - 0">L
</a>
</td>
</tr>
</tbody>
</table>
<br>
<div class="results">RESULT:
</div>
我有一个包含多行和多列的 table。我想要的是:如果 5 个 children 中的前 4 个具有特定的 class,则将特定文本附加到另一个 div。否则,不是。我被这段代码困住了。任何帮助将不胜感激:
$('table > tbody tr').each(function() {
var counter = 0;
var ccc;
$('td.form a:nth-child(1), td.form a:nth-child(2), td.form a:nth-child(3),td.form a:nth-child(4)').each(function() {
if(( $(this).hasClass("form-win") ) || ( $(this).hasClass("form-loss") )) {
$(this).css('background','red');
counter++;
ccc = $(this).closest('tr.team_rank').find('td.team a').text();
$('results').append(ccc);
}
else {
$(this).css('background','blue');
}
});
});
我也更新了我的笔,以可视化 table: https://codepen.io/tudor-taranu/pen/gZrmKR
$('table > tbody tr').each(function() {
var counter = 0;
var ccc;
$('td.form a:nth-child(1), td.form a:nth-child(2), td.form a:nth-child(3),td.form a:nth-child(4)').each(function() {
if(( $(this).hasClass("form-win") ) || ( $(this).hasClass("form-loss") )) {
$(this).css('background','red');
counter++;
ccc = $(this).closest('tr.team_rank').find('td.team a').text();
$('results').append(ccc);
}
else {
$(this).css('background','blue');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="leaguetable sortable table detailed-table" id="page_competition_1_block_competition_tables_7_block_competition_league_table_1_table" data-round_id="48876">
<thead>
<tr class="sub-head">
<th title="Loc" class="sortasc sortdefaultasc">#
</th>
<th class="text team sortdefaultasc">team
</th>
<th class="form nosort">last 5
</th>
</tr>
</thead>
<tbody>
<tr class="odd team_rank" id="team_rank_row_1824" data-team_id="1824">
<td class="rank rank-light-green">1
</td>
<td class="text team large-link">
<a title="CFR Cluj">CFR Cluj
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="CFR Cluj - Gaz Metan Mediaş 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win" title="Dinamo Bucureşti - CFR Cluj 0 - 3" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="CFR Cluj - Voluntari 5 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Concordia Chiajna - CFR Cluj 0 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="CFR Cluj - Dunărea Călăraşi 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_25281" data-team_id="25281">
<td class="rank rank-light-green">3
</td>
<td class="text team large-link">
<a title="Universitatea Craiova">Universitatea …
</a>
</td>
<td class="form">
<a class="form-icon form-win" title="Universitatea Craiova - Voluntari 3 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Concordia Chiajna - Universitatea Craiova 1 - 3" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Universitatea Craiova - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-loss" title="Botoşani - Universitatea Craiova 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw last" title="Universitatea Craiova - Sepsi 1 - 1">D
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_13720" data-team_id="13720">
<td class="rank rank-light-green">4
</td>
<td class="text team large-link">
<a title="Viitorul">Viitorul
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="FCSB - Viitorul 2 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Gaz Metan Mediaş - Viitorul 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win" title="Viitorul - Dinamo Bucureşti 4 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Voluntari - Viitorul 1 - 2" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="Viitorul - Concordia Chiajna 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_1827" data-team_id="1827">
<td class="rank rank-light-green">5
</td>
<td class="text team large-link">
<a title="Gaz Metan Mediaş">Gaz Metan Mediaş
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="CFR Cluj - Gaz Metan Mediaş 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-draw" title="Gaz Metan Mediaş - Viitorul 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="FCSB - Gaz Metan Mediaş 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Dinamo Bucureşti - Gaz Metan Mediaş 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win last" title="Gaz Metan Mediaş - Voluntari 2 - 0">W
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_37490" data-team_id="37490">
<td class="rank rank-orange">10
</td>
<td class="text team large-link">
<a title="Hermannstadt">Hermannstadt
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="Hermannstadt - Dinamo Bucureşti 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="Voluntari - Hermannstadt 2 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-win" title="Hermannstadt - Concordia Chiajna 2 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Dunărea Călăraşi - Hermannstadt 0 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="Hermannstadt - Botoşani 1 - 1">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_1775" data-team_id="1775">
<td class="rank rank-orange">11
</td>
<td class="text team large-link">
<a title="Dinamo Bucureşti">Dinamo Bucureşti
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="Hermannstadt - Dinamo Bucureşti 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="Dinamo Bucureşti - CFR Cluj 0 - 3" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Viitorul - Dinamo Bucureşti 4 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Dinamo Bucureşti - Gaz Metan Mediaş 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-draw last" title="Dinamo Bucureşti - FCSB 1 - 1">D
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_9221" data-team_id="9221">
<td class="rank rank-orange">12
</td>
<td class="text team large-link">
<a title="Dunărea Călăraşi">Dunărea Călăraşi
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Politehnica Iași - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Dunărea Călăraşi - Astra 1 - 2" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Universitatea Craiova - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Dunărea Călăraşi - Hermannstadt 0 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw last" title="CFR Cluj - Dunărea Călăraşi 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_9088" data-team_id="9088">
<td class="rank rank-orange">13
</td>
<td class="text team large-link">
<a title="Concordia Chiajna">Concordia Chiajna
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Concordia Chiajna - Politehnica Iași 3 - 6" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Astra - Concordia Chiajna 3 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Concordia Chiajna - Universitatea Craiova 1 - 3" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Hermannstadt - Concordia Chiajna 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss last" title="Concordia Chiajna - CFR Cluj 0 - 1">L
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_11607" data-team_id="11607">
<td class="rank rank-orange">14
</td>
<td class="text team large-link">
<a title="Voluntari">Voluntari
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Universitatea Craiova - Voluntari 3 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-win" title="Voluntari - Hermannstadt 2 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-loss" title="CFR Cluj - Voluntari 5 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Voluntari - Viitorul 1 - 2" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss last" title="Gaz Metan Mediaş - Voluntari 2 - 0">L
</a>
</td>
</tr>
</tbody>
</table>
<br>
<div class="results">RESULT:
</div>
这是我对你的问题的解决方案:在每个 tr
中,你必须检查每个 form-icon
是否有 class form-win
或 form-loss
(你的红色的),如果有的话加+1来反击。如果计数器达到 4,您知道前 4 个 form-icon
是红色的,您可以将该团队添加到 results
div.
这是脚本,所有注释都是为了更好地理解(我尝试使用您已经编写的代码):
$('table > tbody tr').each(function () {
var counter = 0; /* reset counter to every tr loop */
var ccc = $(this).closest('tr.team_rank').find('td.team a').text(); /* put in ccc the name of team every tr */
$(".form-icon", $(this)).each(function () {
if ($(this).hasClass("form-win") || $(this).hasClass("form-loss")) {
counter++ /*if a form-icon has the class form-win or form-loss (the red ones) add +1 to counter */
} else {
return false; /* if those classes there aren't exit from loop */
}
if (counter == 4) {
$('.results').append(ccc + ", "); /*if the counter is equals to 4 add the team to .result div */
}
});
});
这是正在运行的脚本。
$('table > tbody tr').each(function () {
var counter = 0; /* reset counter to every tr loop */
var ccc = $(this).closest('tr.team_rank').find('td.team a').text(); /* put in ccc the name of team every tr */
$(".form-icon", $(this)).each(function () {
if ($(this).hasClass("form-win") || $(this).hasClass("form-loss")) {
counter++ /*if a form-icon has the class form-win or form-loss (the red ones) add +1 to counter */
} else {
return false; /* if those classes there aren't exit from loop */
}
if (counter == 4) {
$('.results').append(ccc + ", "); /*if the counter is equals to 4 add the team to .result div */
}
});
});
/* This script removes last ', ' from your teams's string. You can remove it, if you don't need it */
$('.results').text(function (_, txt) {
return txt.slice(0, -2);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="leaguetable sortable table detailed-table" id="page_competition_1_block_competition_tables_7_block_competition_league_table_1_table" data-round_id="48876">
<thead>
<tr class="sub-head">
<th title="Loc" class="sortasc sortdefaultasc">#
</th>
<th class="text team sortdefaultasc">team
</th>
<th class="form nosort">last 5
</th>
</tr>
</thead>
<tbody>
<tr class="odd team_rank" id="team_rank_row_1824" data-team_id="1824">
<td class="rank rank-light-green">1
</td>
<td class="text team large-link">
<a title="CFR Cluj">CFR Cluj
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="CFR Cluj - Gaz Metan Mediaş 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win" title="Dinamo Bucureşti - CFR Cluj 0 - 3" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="CFR Cluj - Voluntari 5 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Concordia Chiajna - CFR Cluj 0 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="CFR Cluj - Dunărea Călăraşi 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_25281" data-team_id="25281">
<td class="rank rank-light-green">3
</td>
<td class="text team large-link">
<a title="Universitatea Craiova">Universitatea …
</a>
</td>
<td class="form">
<a class="form-icon form-win" title="Universitatea Craiova - Voluntari 3 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Concordia Chiajna - Universitatea Craiova 1 - 3" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Universitatea Craiova - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-loss" title="Botoşani - Universitatea Craiova 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw last" title="Universitatea Craiova - Sepsi 1 - 1">D
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_13720" data-team_id="13720">
<td class="rank rank-light-green">4
</td>
<td class="text team large-link">
<a title="Viitorul">Viitorul
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="FCSB - Viitorul 2 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Gaz Metan Mediaş - Viitorul 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win" title="Viitorul - Dinamo Bucureşti 4 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Voluntari - Viitorul 1 - 2" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="Viitorul - Concordia Chiajna 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_1827" data-team_id="1827">
<td class="rank rank-light-green">5
</td>
<td class="text team large-link">
<a title="Gaz Metan Mediaş">Gaz Metan Mediaş
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="CFR Cluj - Gaz Metan Mediaş 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-draw" title="Gaz Metan Mediaş - Viitorul 2 - 2" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="FCSB - Gaz Metan Mediaş 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Dinamo Bucureşti - Gaz Metan Mediaş 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-win last" title="Gaz Metan Mediaş - Voluntari 2 - 0">W
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_37490" data-team_id="37490">
<td class="rank rank-orange">10
</td>
<td class="text team large-link">
<a title="Hermannstadt">Hermannstadt
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="Hermannstadt - Dinamo Bucureşti 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="Voluntari - Hermannstadt 2 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-win" title="Hermannstadt - Concordia Chiajna 2 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-win" title="Dunărea Călăraşi - Hermannstadt 0 - 1" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-draw last" title="Hermannstadt - Botoşani 1 - 1">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_1775" data-team_id="1775">
<td class="rank rank-orange">11
</td>
<td class="text team large-link">
<a title="Dinamo Bucureşti">Dinamo Bucureşti
</a>
</td>
<td class="form">
<a class="form-icon form-draw" title="Hermannstadt - Dinamo Bucureşti 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-loss" title="Dinamo Bucureşti - CFR Cluj 0 - 3" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Viitorul - Dinamo Bucureşti 4 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw" title="Dinamo Bucureşti - Gaz Metan Mediaş 1 - 1" style="background: blue none repeat scroll 0% 0%;">D
</a>
<a class="form-icon form-draw last" title="Dinamo Bucureşti - FCSB 1 - 1">D
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_9221" data-team_id="9221">
<td class="rank rank-orange">12
</td>
<td class="text team large-link">
<a title="Dunărea Călăraşi">Dunărea Călăraşi
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Politehnica Iași - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Dunărea Călăraşi - Astra 1 - 2" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Universitatea Craiova - Dunărea Călăraşi 1 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Dunărea Călăraşi - Hermannstadt 0 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-draw last" title="CFR Cluj - Dunărea Călăraşi 0 - 0">D
</a>
</td>
</tr>
<tr class="odd team_rank" id="team_rank_row_9088" data-team_id="9088">
<td class="rank rank-orange">13
</td>
<td class="text team large-link">
<a title="Concordia Chiajna">Concordia Chiajna
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Concordia Chiajna - Politehnica Iași 3 - 6" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Astra - Concordia Chiajna 3 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Concordia Chiajna - Universitatea Craiova 1 - 3" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Hermannstadt - Concordia Chiajna 2 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss last" title="Concordia Chiajna - CFR Cluj 0 - 1">L
</a>
</td>
</tr>
<tr class="even team_rank" id="team_rank_row_11607" data-team_id="11607">
<td class="rank rank-orange">14</td>
<td class="text team large-link">
<a title="Voluntari">Voluntari
</a>
</td>
<td class="form">
<a class="form-icon form-loss" title="Universitatea Craiova - Voluntari 3 - 1" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-win" title="Voluntari - Hermannstadt 2 - 0" style="background: red none repeat scroll 0% 0%;">W
</a>
<a class="form-icon form-loss" title="CFR Cluj - Voluntari 5 - 0" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss" title="Voluntari - Viitorul 1 - 2" style="background: red none repeat scroll 0% 0%;">L
</a>
<a class="form-icon form-loss last" title="Gaz Metan Mediaş - Voluntari 2 - 0">L
</a>
</td>
</tr>
</tbody>
</table>
<br>
<div class="results">RESULT:
</div>