Using server-side includes, what are options for selecting a current nav element?

Deadly 提交于 2019-12-04 18:26:25

For example. You hame some pages:

And you menu has a next form:

<ul>
    <li class="index">index</li>
    <li class="faq">faq</li>
    <li class="forum">forum</li>
</ul>

Now you check the url of your page.

var index = window.location.pathname.split('/')[1];
$('ul.li').removeClass('active');
$('li.' + index).addClass('active');

Profit!

First detect the page name

var index = document.location.href.lastIndexOf("/") + 1;
var page = document.location.href.substring(index,document.location.href.length);

Than find the li with same name and add class active to it considering the navbar has class navbar in the ul.

$('ul.navbar li').each(function() {
    $(this).removeClass('active');   
    if($.trim($(this).text()) == page) {
        $(this).addClass('active');
    } 
});

This will fulfill your requirement. NOTE: I have assumed li name same as page name

You can first look at the url to detect the page name, then you can match the page name to classes you define on your LIs.

if(document.location.href.indexOf('index')>-1) {
      $('li.index').addClass('active')
}

You can also do this with the page title instead if you wish, instead of the url.

Another trick is to add a class to the BODY tag, per page, and let CSS do the work:

body.index li.index {
    background-color:#FF0000
}

body.aboutus li.aboutus {
  .... whatever
}

Here is a link to a jquery/javascript free solution

http://www.webtrainingcentre.com/server-side-includes/tutorials/create-active-links-using-common-include/

I am sure this will help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!