jquery - Javascript to compare page you are on with a link -


i have menu, , need link current page on highlighted differently. twisted way of comparing strings now, sure there smarter way?

  jquery(document).ready (function(){         jquery(".filteroptions .option").each (function (index,ele)          {                 link=jquery(ele).find("a").attr("href").tostring();                 p=jquery(window).attr("location").tostring().search(link)                 if (p!=-1){                         jquery(ele).find(".link").addclass("current")                 }         });   }); 

something work...

$('a').each(function(){     if ( $(this).attr('href') == window.location.href ) {         $(this).addclass('current');     } }); 

or if you're not using full-path urls in links...

$('a').each(function(){     if ( window.location.href.indexof( $(this).attr('href') ) !== -1 ) {         $(this).addclass('current');     } }); 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -