That code has several mistakes:
this
refers to the document, not on any li
element.
- Setting classes with
.attr
is a really bad choice -- addClass
, removeClass
and toggleClass
exist exactly for this purpose.
- You do not even need jQuery for this, as it can be done with pure CSS.
In jQuery form, what you want is more like
$('.navigation li:not(.selected)').hover(
function () {
$(this).find('a').addClass('hovering');
},
function () {
$(this).find('a').removeClass('hovering');
});
If you want to make the script respond to changes in the selected
class dynamically another version would be the solution:
$('.navigation li').hover(
function () {
if(!$(this).hasClass('selected')) $(this).find('a').addClass('hovering');
},
function () {
if(!$(this).hasClass('selected')) $(this).find('a').removeClass('hovering');
});
Of course there is no reason to do this with jQuery; pure CSS will suffice:
.navigation li:not(.selected):hover a {
/* your styles go here */
}
The :not
selector is not supported by IE8, but if that is a problem you can work around it using the set/revoke trick.