not选择器可以和'this'一起使用吗?

Can the :not selector be used with 'this'?

本文关键字:一起 this not 选择器      更新时间:2023-09-26

我有一个称为navdiv,其中放置了一些其他div。当我点击一个,我想把它的颜色改为橙色,这是很好的-使用this

我希望其他的在没有点击的时候保持黑色。

not能与this一起使用吗?

$('.nav div').click(function() {
    $(this).css('color', 'orange');
    $('.nav div:not(this)').css('color', 'black');
});

您可以使用.not()方法代替选择器:

$(".nav div").not(this).css("color", "black");

或者你可以使用.siblings()方法

$(this).siblings().css("color", "black");

你可以添加一个类到你所有的div,当你点击一个,用jQuery删除它与

.removeClass("<name>");

我不这么认为。为什么不将它们全部设置为黑色,然后将特殊的设置为橙色呢?

$('.nav').css('color', 'black');
$(this).css('color', 'orange');