Can the :not selector be used with 'this'?
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');
精彩评论