• contac...">
    All geek questions in one place

    JQuery "active" class assignment for parent

    I have this html in my main navigation:

    <div id="navigation">  
      <ul>
         <li>
           <a href="/contact">contact us</a>
         </li>
       <li>
          <a href="/projects">projects</a>       
       </li>
      </ul>
     </div>
    

    I need to mark the active link, so I use this jquery:

    $("div#navigation").find("a[href='" + top.location.pathname + "']").each(function ()
     {
       $(this).addClass("active")
     })
    

    while it works well for URLs like domain.com/projects or domain.com/contacts,
    I have a problem for deeper URLs, let's take domain.com/projects/proj-1 for example.
    I want that when I go to a deeper URL, it will be marked as the active parent URL (domain.com/projects). How can i do this?

    Thank!

    +3
    jquery
    eran Dec 28 '10 at 0:52
    source share
    2 answers

    First of all, in your example, you do not need to use a method .each(), since you can directly assign a class like this

    $("div#navigation").find("a[href='" + top.location.pathname + "']").addClass("active")
    

    $("div#navigation").find("a").filter(function(){
        var href =  $(this).attr('href');
        return href!='/' && href !='#' && top.location.pathname.indexOf( href )==0 ;
    }).addClass("active");
    

    edit: , .

    +1
    Gabriele Petrioli 28 . '10 1:10

    jQuery closest , .

    $(this).closest("li","#navigation").addClass("active");    
    

    , , .

    , , . :

    $("div#navigation").find("a[href='" + top.location.pathname + "']").addClass("active");
    

    , :

    $selected = $("div#navigation").find("a[href='" + top.location.pathname + "']");
    $selected.addClass("active");
    $selected.closest("li","#navigation").addClass("active");
    
    0
    Keltex 28 . '10 1:04

    Source: https://habr.com/ru/post/1782380/

    More articles:

    • https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1782375/i-want-to-roll-3-dice-with-independent-numbers-from-each-other&usg=ALkJrhjkxmvtNqxcOMkYKXvch0iE6r1xYg
    • Mixed read / write and read-only rows in Telerik Ajax RadGrid? - asp.net
    • In KRL How can I get the current year, month and day? - time
    • https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1782378/win32-object-cleanup-and-global-variables&usg=ALkJrhi5Chjt8oOjMEPkjPQ1BAhG3LiHFg
    • Qt how to wait / find out when QMainWindow is closed? - c ++
    • Stuck PHP date() показывает тот же день/время - date
    • Web Application Development Platform Recommendation - web-applications
    • Is there a way to get older versions of Apple Sample Code? - cocoa
    • Wrapping text inside a table cell - html
    • help organize my data for this machine learning problem - machine-learning

    All Articles

    Geek Questions | 2019