Posted By

iammikecohen on 07/09/11


Tagged

css javascript navigation jquery


Versions (?)

Set links with href to current page as active (or if in subnav set their parent to active)


 / Published in: JavaScript
 

look through the list items in your main nav and based on href of anchor tags this will set the top level link to have an 'active' class.

  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3. $(".sf-menu li").children("a").each(function() {
  4. if(window.location.href == $(this).attr('href')){
  5. if($(this).parent().hasClass('main_link')){
  6. $(this).addClass('active');
  7. }else{
  8. var topLink = $(this).parents('.main_link');
  9. topLink.find('a').first().addClass('active')
  10. }
  11. }
  12. });
  13. });
  14. </script>

Report this snippet  

You need to login to post a comment.