gpt4 book ai didi

javascript - 逻辑非运算符 (!) 将类添加到错误按钮的问题

转载 作者:行者123 更新时间:2023-12-01 02:27:48 26 4
gpt4 key购买 nike

我有一个按钮列表。当其中一个按钮被单击时,我希望为所有其他按钮提供一个 not-selected 类,这会将其文本的颜色更改为灰色并使指针事件:无

我正在使用点击处理程序,不确定我使用的感叹号 ! 是否正确,因为它不断将该类添加到我刚刚单击的按钮,而不是所有未单击的按钮点击。

$(".btn--list").on("click", function(){
!($(this).addClass("not-selected"));
)};
<ul>
<li><h2>Filters</h2></li>
<li class="filter--all"><button class="btn--list btn--all is-selected">Select All</button></li>

<li><h2>Decade</h2></li>
<li><button class="btn--list btn--1960">1960s</button></li>
<li><button class="btn--list btn--1970">1970s</button></li>
<li><button class="btn--list btn--1980">1980s</button></li>
<li><button class="btn--list btn--1990">1990s</button></li>
<li><button class="btn--list btn--2000">2000s</button></li>
<li><button class="btn--list btn--2010">2010s</button></li>

<li><h2>Position</h2></li>
<li><button class="btn--list btn--goalie">Goalie</button></li>
<li><button class="btn--list btn--defencemen">Defenceman</button></li>
<li><button class="btn--list btn--forward">Forward</button></li>

<li><h2>Other</h2></li>
<li><button class="btn--list btn--name">First Name</button></li>
<li><button class="btn--list btn--hometown">Hometown</button></li>
<li><button class="btn--list btn--gamesp">Games Played</button></li>
<li class="not--goalie not--goals"><button class="btn--list btn--goals">Goals</button></li>
<li class="not--goalie not-assists"><button class="btn--list btn--assists">Assists</button></li>
<li class="not--goalie not-points"><button class="btn--list btn--points">Points</button></li>
<li class="not--goalie not-penalty"><button class="btn--list btn--penalty">Penalty Minutes</button></li>
<li><button class="btn--list btn--goalsagainst">Goals Against <span class="is-highlighted">*</span></button></li>
<li><button class="btn--list btn--savepercent">Save Percentage <span class="is-highlighted">*</span></button></li>
<li><button class="btn--list btn--shutouts">Shutouts <span class="is-highlighted">*</span></button></li>
</ul>

最佳答案

您在 jQuery 对象上使用 ! 运算符是无效的。要实现您的要求,您可以选择所有 .btn--list 元素,并使用 .not(this) 排除当前元素。试试这个:

$(".btn--list").on("click", function() {
$(".btn--list").not(this).addClass("not-selected");
});
/* For demo purposes... */
.not-selected {
background-color: #c00;
pointer-events: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>
<h2>Filters</h2>
</li>
<li class="filter--all">
<button class="btn--list btn--all is-selected">Select All</button>
</li>

<li>
<h2>Decade</h2>
</li>
<li>
<button class="btn--list btn--1960">1960s</button>
</li>
<li>
<button class="btn--list btn--1970">1970s</button>
</li>
<li>
<button class="btn--list btn--1980">1980s</button>
</li>
<li>
<button class="btn--list btn--1990">1990s</button>
</li>
<li>
<button class="btn--list btn--2000">2000s</button>
</li>
<li>
<button class="btn--list btn--2010">2010s</button>
</li>

<li>
<h2>Position</h2>
</li>
<li>
<button class="btn--list btn--goalie">Goalie</button>
</li>
<li>
<button class="btn--list btn--defencemen">Defenceman</button>
</li>
<li>
<button class="btn--list btn--forward">Forward</button>
</li>

<li>
<h2>Other</h2>
</li>
<li>
<button class="btn--list btn--name">First Name</button>
</li>
<li>
<button class="btn--list btn--hometown">Hometown</button>
</li>
<li>
<button class="btn--list btn--gamesp">Games Played</button>
</li>
<li class="not--goalie not--goals">
<button class="btn--list btn--goals">Goals</button>
</li>
<li class="not--goalie not-assists">
<button class="btn--list btn--assists">Assists</button>
</li>
<li class="not--goalie not-points">
<button class="btn--list btn--points">Points</button>
</li>
<li class="not--goalie not-penalty">
<button class="btn--list btn--penalty">Penalty Minutes</button>
</li>
<li>
<button class="btn--list btn--goalsagainst">Goals Against <span class="is-highlighted">*</span>
</button>
</li>
<li>
<button class="btn--list btn--savepercent">Save Percentage <span class="is-highlighted">*</span>
</button>
</li>
<li>
<button class="btn--list btn--shutouts">Shutouts <span class="is-highlighted">*</span>
</button>
</li>
</ul>

关于javascript - 逻辑非运算符 (!) 将类添加到错误按钮的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38866352/

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