1

My navigation highlights the section of the page the visitor is on and changes as they scroll by adding .active-nav to ANY href that points to that section of the page. I have an image sprite that points to #foo but I don't want .active-nav added to that anchor tag. I am having no luck implementing the :not selector. I fear I have spent too much time on this and cannot see the obvious answer.

I would like to have the following code ignore an href tag if a specific class (.bar) already exists. Please advise.

if ( windowpos > $('#foo').offset().top) {
$('nav li a').removeClass('active-nav');
$('a[href$="#foo"]').addClass('active-nav');
}
LyleCrumbstorm
  • 171
  • 1
  • 12

2 Answers2

4

You can use the not() function:

$('a[href$="#foo"]').not('.bar').addClass('active-nav');
BenM
  • 52,573
  • 26
  • 113
  • 168
0

How about this:

if($('a[href$="#foo"]').hasClass("bar")){
   //do stuff
} else {
   $('a[href$="#foo"]').addClass('active-nav');
}
renakre
  • 8,001
  • 5
  • 46
  • 99