选择所有输入元素,但不选择当前输入元素

select all input elements but not the current one

本文关键字:选择 输入 元素      更新时间:2023-09-26

这个 Meteor 客户端Template.abc.events代码尝试对所有输入元素应用一个类,但不对触发事件的输入元素应用一个类。
如果不这样做,如何解决?谢谢

  'click input[name="a.b"]': function(event) {
    $('input:not($(event.target))').each(function(element) {
      $(element).addClass('inactive');
    })
  }

$(event.target) 是一个字符串并且没有被计算,它将被视为选择器。您可以使用从集合中排除传递的元素的 .not 方法:

$('input').not(event.target).each(fn);

另请注意,.each回调的第一个参数是索引。each循环无法选择迭代的当前项。应使用回调的第二个参数或 this 关键字。但是,当.addClass循环访问集合时,无需使用 .each 方法。您可以简单地在片场调用.addClass

试试这个$('input').not(event.target)

您可以使用

input:not(:focus)

$(document).ready(function() {
    $('input').click(function() {
        $(this).removeClass('inactive');
        $('input:not(:focus)').addClass('inactive');
    });
});

工作样品:

$(document).ready(function() {
  $('input').click(function() {
    $(this).removeClass('inactive');
    $('input:not(:focus)').addClass('inactive');
  });
});
.inactive {
    border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input />
<input />
<input />
<input />