开发者

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

开发者 https://www.devze.com 2023-04-12 22:04 出处:网络
I\'ve got a div called nav, in which some other divs are placed. When I click on one, I want it to change the colour to orange, which is fine - using thi开发者_开发知识库s.

I've got a div called nav, in which some other divs are placed. When I click on one, I want it to change the colour to orange, which is fine - using thi开发者_开发知识库s.

I'd like the others to remain black when they aren't clicked though.

Can not be used with this?

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


You can use the .not() method instead of the selector:

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

Or you could use the .siblings() method

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


You can add a class to all your divs and when you click on one, remove it with jQuery with

.removeClass("<name>");

Be sure to re-add it when you click on another one.


I don't think so. Why not set them all to black, then set the special one to orange?

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号