How to activate the current page navigation using javascript.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.