jquery - How to add a class active in a menu where url has the same pass -
i have following menu.
<ul id="nav" class="grid_16"> <li><a href="#">forside</a></li> <li><a href="#">mobil</a></li> <li><a href="#">pc og servere</a></li> <li><a href="#">tjenester</a></li> <li><a href="#">diverse</a></li> <li><a href="#">om oss</a></li> <li><a href="#">kontakt</a></li> <li><a href="#">nettbutikk</a></li> </ul> a url path have under score _ there space. example pc og servere
http://www.mywebsite.no/pc_og_servere.asp diverse
http://www.mywebsite.no/diverse.asp i'd add class="active" in list jquery. example when in http://www.mywebsite.no/om_oss.asp
..... ..... <li class="active"><a href="#">om oss</a></li> .... how can it?
thanks in advance.
you this:
var text = window.location.href.match(/http:\/\/www\.mywebsite\.no\/(.+)\.asp/)[1].replace(/_/g,' '); $("#nav li").filter(function() { return $.text([this]) == text; }).addclass("active"); you can change regex, etc...but concept same, text, compare each <li>'s text (since <a> adds none, can right on <li>). much older versions of jquery you'll need use .text() instead of shortcut above, this:
$("#nav li").filter(function() { return $(this).text() == text; }).addclass("active"); note we're not using :contains() here, we're doing exact match, instead of substring.
Comments
Post a Comment