Friday 15 August 2014

javascript - Call JQuery function after link to a page -


For my navigation bar, I have that when I click on any item, my CSS class " The current menu -item "is active. It works to some degree. My Jquery code is as follows:

  $ (document) .ready (function () {$ (" li.menu ") .removeClass ("current menu item"); $ (this) .addClass ("current menu item");});}).   

This train links to:

  & lt; Li class = "menu" & gt; & Lt;% = link_to "Home", root_url% & gt; & Lt; / Li & gt;   

The only problem with this is that this code executes before the redirect. So I can only activate the class for a brief second before the new page loads and it has been removed.

What is the easiest way to solve this dilemma?

If you are going to do this with javascript, not on the server, then you window .location.pathname href featuring your menu links.

Something like this:

  $ Each ("li.menu", function (I, menuItem) {if (menuItem.attr ("href") === window.location.pathname} {menuItem.addClass ("current menu item");}});   

Keep in mind that until your path name exactly matches the href attribute, this will not work, so this is likely to be error.

No comments:

Post a Comment