How to highlight active tab on the website menu?

2019-08-09 07:04发布

My question is : I have a menu items, and I want to highlight the active tab that users switch to that points to another page for sure .

stackover flow use :

.nav {
    float: left;
    font-size: 125%;
}
.nav ul {
    margin: 0;
}
.nav li {
    background: none repeat scroll 0 0 #777777;
    display: block;
    float: left;
    margin-right: 7px;
}
**.nav .youarehere {
    background: none repeat scroll 0 0 #FF9900;
}**
.youarehere a {
    color: #FFFFFF;
}
.nav li:hover {
    background-color: #FF9900;
}
.nav a {
    color: #FFFFFF;
    display: block;
    padding: 6px 12px;
    text-decoration: none;
}

Can anybody tell me what else they use to make this work ?

1条回答
【Aperson】
2楼-- · 2019-08-09 07:37

Well one way to do it with Javascript that I've used before on my pages,

Put all of your sidebar buttons in a CSS class called sideBarButton. activeSideBarButton will be a class that gets set when the link is the same as the current window's location.

$(document).ready(function () {
    var sideBarButtons = $(".sideBarButton");
    for(var i in sideBarButtons)
    {   
      if(!sideBarButtons[i].pathname)
      {   
        continue;
      }   
      if(sideBarButtons[i].pathname == window.location.pathname)
      {   
        sideBarButtons[i].className += ' activeSideBarButton';
        console.log("Enabled button " + sideBarButtons[i]);
      }   
    } 
});
查看更多
登录 后发表回答