Navigation in django

后端 未结 30 1420

I\'ve just done my first little webapp in django and I love it. I\'m about to start on converting an old production PHP site into django and as part its template, there is a

30条回答
  •  情歌与酒
    2020-11-27 09:59

    I also used jQuery to highlight it and find it more elegant than cluttering the template with non-semantic Django template tags.

    The code below works with nested dropdowns in bootstrap 3 (highlights both the parent, and the child

  • element.

    // DOM Ready
    $(function() {
        // Highlight current page in nav bar
        $('.nav, .navbar-nav li').each(function() {
            // Count the number of links to the current page in the 
  • var matched_links = $(this).find('a[href]').filter(function() { return $(this).attr('href') == window.location.pathname; }).length; // If there's at least one, mark the
  • as active if (matched_links) $(this).addClass('active'); }); });
  • It's also quite easy to add a click event to return false (or change the href attribute to #) for the current page, without changing the template/html markup:

            var matched_links = $(this).find('a[href]').filter(function() {
                var matched = $(this).attr('href') == window.location.pathname;
                if (matched)
                    $(this).click(function() { return false; });
                return matched;
            }).length;
    

提交回复
热议问题