gpt4 book ai didi

javascript - 无法删除/将元素类别切换回 `is-inactive`

转载 作者:行者123 更新时间:2023-11-28 18:27:18 24 4
gpt4 key购买 nike

我在哪里

选择曲棍球运动员后,将其默认的 is-inactive 类别切换为 is-active 来表示。我还添加了 full 类,以防止选择 picked.is-inactive 类的玩家。

问题

一旦达到最大数量的玩家(四名守门员中的两名、十五名防守队员中的六名以及已达到该类别中 31 名前锋中的 12 名,以便“取消选择”一名球员并将其类别切换回默认的 is-inactive,然后选择另一名球员。

为了解决这个问题,我觉得我需要在函数中使用 .removeClass() 和 if-else 语句?

脚本.js

    /*-------------------------------------
COUNT SELECTED
--------------------------------------*/

function countSelected() {
$(".player").on("click", function(){

// Checks if the maximum number of players have been selected
// If so, return false and then do nothing
// If not, the class will toggle from `is-inactive` to `is-active`
if ($(this).find(".picked.full").length > 0) return false;
$(this).find(".picked").toggleClass("is-inactive is-active");

// Count the number of players with stars
var starredGoaltenders = $(".player--goalie").find(".picked.is-active").length;
var starredDefencemen = $(".player--defencemen").find(".picked.is-active").length;
var starredForwards = $(".player--forward").find(".picked.is-active").length;

console.log(starredGoaltenders, starredDefencemen, starredForwards);

// The number of starred players for each position cannot exceed the following numbers
var maxGoaltenders = 2;
var maxDefencemen = 6;
var maxFowards = 12;

// If the number of starred players hits its max, a class of `is-completed` is adding to the corresponding checkmark to indicate that the task has been completed
if (starredGoaltenders === maxGoaltenders) {
$(".checkmark--goalie").addClass("is-completed");
$(".player--goalie").find(".picked").addClass("full");
}

if (starredDefencemen === maxDefencemen) {
$(".checkmark--defencemen").addClass("is-completed");
$(".player--defencemen").find(".picked").addClass("full");
}

if (starredForwards === maxFowards) {
$(".checkmark--forward").addClass("is-completed");
$(".player--forward").find(".picked").addClass("full");
}

// If all the conditions are met show the submit vote button
if (starredGoaltenders === maxGoaltenders && starredDefencemen === maxDefencemen && starredForwards === maxFowards) {
$(".btn--submit").show();
$(".btn--submit").addClass("slideLeft");
}
});
} countSelected();

index.html

<div class="player player--goalie year--1970">
<div class="tooltip tooltip--tall">
<p class="tooltip__name">Glen Hanlon</p>
<p class="tooltip__hometown"><span>Hometown:</span> Brandon, Man.</p>
<p class="tooltip__years"><span>Years Played:</span> 1974-1977</p>
<div class="tooltip__stats--inline">
<div class="stats__group stats--games">
<p class="stats__header">GP</p>
<p class="stats__number--games">172</p>
</div>

<div class="stats__group stats--goalsag">
<p class="stats__header">GA</p>
<p class="stats__number">4.22</p>
<p class="stats__number">3.99</p>
<p class="stats__number stats__number--goalsag">3.09</p>
</div>

<div class="stats__group stats--savep">
<p class="stats__header">SAV%</p>
<p class="stats__number">.892</p>
<p class="stats__number">.891</p>
<p class="stats__number stats__number--savep">.906</p>
</div>

<div class="stats__group stats--shutouts">
<p class="stats__header">SO</p>
<p class="stats__number">0</p>
<p class="stats__number stats__number--shutouts">4</p>
<p class="stats__number">4</p>
</div>
</div> <!-- tooltip__stats--inline -->
</div> <!-- tooltip -->
<div class="player__headshot player--hanlon">
<div class="picked is-inactive"><i class="fa fa-star" aria-hidden="true"></i></div>
</div>
<p class="player__name">Glen Hanlon</p>
<p class="player__position">Goalie</p>
</div>

最佳答案

当类 full 存在时,您正在执行return false,但当选择数量未满时,您不会删除它。

尝试像这样return false:

if ($(this).find(".picked.is-inactive.full").length > 0) return false;

然后像这样执行elseremoveClass

$(".player--goalie").find(".picked").removeClass("full");

完整示例在这里 https://jsfiddle.net/5pq8z54z/

关于javascript - 无法删除/将元素类别切换回 `is-inactive`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38876472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com