How to activate the current page navigation using javascript.
This example describes how to activate the current page navigation using javascript. Share it with you for your reference. The details are as follows:
Html section (reference navigation, unavailable # Replace link, test how to create several static pages)
<Ul id = "nav"> <li> <a href = "/"> homepage </a> </li> <a href = "/category/html -css/"> HTML/CSS </a> </li> <a href ="/category/javascript/"> JavaScript </a> </li> <li> <a href = "/category/seo/"> SEO </a> </li> <a href = "/category/front-end /"> front-end news </a> </li> </ul>
Javascript section (add the. on class to the specified navigation on the current page)
$(function() {var a1 = document.URL;var a2 = $("#nav a");for (var i = 0; i < a2.length; i++) {if (a1.indexOf($(a2[i]).attr("href")) != -1) {$(a2[i]).parent().addClass("on");return;}}$(a2[0]).parent().addClass("on");})
Css section (you can adjust it based on your needs)
#nav li{display:inline-block;float:left;text-align:center;height:36px;padding-left:4px;line-height:36px;background:url(images/nav.gif) no-repeat right bottom;}#nav li a{display:block;color:#777;padding:0 15px 0 10px;}#nav li.on{font-weight:bold;background:url(images/nav.gif) no-repeat 0 0;margin-left:-3px;}#nav li.on a{background:url(images/nav.gif) no-repeat right 0;}
I hope this article will help you design javascript programs.