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

时间:2016-08-10 07:01:26

标签: javascript jquery css button negation

我有一个按钮列表。当点击其中一个时,我想给所有其他按钮一个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>

1 个答案:

答案 0 :(得分:7)

您在jQuery对象上使用.btn--list运算符无效。为了达到您的要求,您可以选择所有.not(this)元素,并使用$(".btn--list").on("click", function() { $(".btn--list").not(this).addClass("not-selected"); });排除当前元素。试试这个:

&#13;
&#13;
/* For demo purposes... */
.not-selected {
  background-color: #c00;
  pointer-events: none;
}
&#13;
<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>
&#13;
{{1}}
&#13;
&#13;
&#13;