Reputation: 4095
I want to add .hover() to every anchor in my website, except those that already have .hover() [added .hover() separately before].
I have no idea how to do it in jQuery or if its possible.
If its not possible, I have another idea but again, I dont know how to do it or if its possible.
The idea goes like that: can I add .hover() effect to every anchor except those are in parent div with id "test" and those are in parent div with id "test2"?
Thanks!.
Upvotes: 0
Views: 291
Reputation: 25776
Many ways to do this, some of which are.
First
$('a').not('#test a, #test2 a');
Second
var divs_to_test = $('#test').add('#test2');
$('a').filter(function(){
return !$(this).parent().is(divs_to_test);
});
Upvotes: 2
Reputation: 253308
The easiest way I found for this is the somewhat less-than-concise:
$('a').filter(
function() {
if (!$(this).closest('div[id^="test"]').length) {
return this;
}
}).hover(
function(){
$(this).css('background-color','red');
},
function(){
$(this).css('background-color','white');
});
Upvotes: 0
Reputation: 887365
You can write
$('a').not($('#test a, #test2 a'))
To select all <a>
s that are not in those elements.
Upvotes: 1