.not 具有多个变量的选择器



有没有办法在jQuery的.not()选择器中使用多个变量?

例如

var target = $('.custom-element') // This must be in a variable
$('.element').not(this, target).toggleClass('visible');

不能将多个参数传递给.not() 。但是,您可以链接它们

$('.element').not(target).not(this).toggleClass('visible');

您可以使用.add()将元素this添加到现有组,然后才能使用它。

var target = $('.custom-element') ;
$('.element').not(target.add(this)).toggleClass('visible');

为了简单起见,您也可以尝试一下。CSS 规则允许使用逗号分隔的选择器:

var target = $('.custom-element') // This must be in a variable
$('.element, .custom-element').not(this).toggleClass('visible');

最新更新