jQuery: not() in Javascript



我有简单的jQuery代码,但我在任何地方都找不到如何在新的Javascript中从jQuery编写not((。

有什么选择可以做到这一点吗?

我的代码:

$('.chevron').click(function() {
var $ptag = $(this).closest('.column').find('p');
$('.chevron.active').not(this).removeClass('active');
$('p.active').not($ptag).removeClass('active');
$(this).toggleClass('active');
$ptag.toggleClass('active');
});

没有内置的快捷方式,您必须循环访问集合并删除与单击的.chevron匹配的项目,或其关联的<p>之一:

const chevrons = [...document.querySelectorAll('.chevron.active')];
const chevronsNotThis = chevrons.filter(chev => chev !== this);
const thesePs = [...this.closest('.column').querySelectorAll('p')];
const activePs = [...document.querySelectorAll('p.active')];
const activePsNotThis = activePs.filter(activeP => !thesePs.includes(activeP));

最新更新