Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

highlight the navigation menu for the current page

Tags:

In a page with some navigation links,I want the link of the current page are hightlighted,just like this:

alt text

The link "HTML Attributes" is highlighted(bolded) since this link will take one to the current page.

I know this can be implemented manually(just hightlighted the according link,but is there some smart way? highlight the right link dynamically and automatically?

like image 853
hguser Avatar asked Jan 07 '11 14:01

hguser


People also ask

How do I highlight the current page in WordPress navigation menu?

Highlighting the current page makes navigation easier. WordPress menu functions (wp_nav_menu, wp_list_pages) automatically add current_page_item class to li containing the active link. So all we have to do is use the same class to highlight the current page.

How do you highlight the current link in CSS?

The :active selector is used to select and style the active link. A link becomes active when you click on it. Tip: The :active selector can be used on all elements, not only links.

How do I add a navigation menu to my website?

Create a new menuIn the left sidebar menu, navigate to Website > Navigation. Your default menu contains the pages that will automatically populate the default content of an advanced menu module. Use the dropdown menu to select an existing menu to update. To create a new menu, click + Add menu.


1 Answers

CSS:

.topmenu ul li.active a, .topmenu ul li a:hover {     text-decoration:none;     color:#fff;     background:url(../images/menu_a.jpg) no-repeat center top; } 

JavaScript:

<script src="JavaScript/jquery-1.10.2.js" type="text/javascript"></script>   <script type="text/javascript">     $(function() {         // this will get the full URL at the address bar         var url = window.location.href;          // passes on every "a" tag         $(".topmenu a").each(function() {             // checks if its the same on the address bar             if (url == (this.href)) {                 $(this).closest("li").addClass("active");                 //for making parent of submenu active                $(this).closest("li").parent().parent().addClass("active");             }         });     });         </script> 

Html Code:

<div class="topmenu">     <ul>         <li><a href="Default.aspx">Home</a></li>         <li><a href="NewsLetter.aspx">Newsletter</a></li>         <li><a href="#">Forms</a></li>         <li><a href="#">Mail</a></li>         <li><a href="#">Service</a></li>         <li style="border:none;"><a href="#">HSE</a></li>         <li><a href="#">MainMenu2</a>            <ul>               <li>submenu1</li>               <li>submenu2</li>               <li>submenu3</li>           </ul>        </li>     </ul> </div> 
like image 56
Raj Avatar answered Sep 23 '22 01:09

Raj