JavaScript implements the current page navigation activation method _javascript Tips

Source: Internet
Author: User

This example describes the way JavaScript implements the current pager activation. Share to everyone for your reference. Specifically as follows:

HTML section (referenced navigation, no # Instead of links, test can create a few static pages)

<ul id= "NAV" >
<li><a href= "/" > Home </a></li>
<li><a href= "/category/ html-css/">HTML/CSS</a></li>
<li><a href="/category/javascript/">javascript</ a></li>
<li><a href= "/category/seo/" >SEO</a></li>
<li><a href= "/category/front-end/" > Front-end news </a></li>
</ul>

JavaScript section (specifies the navigation for the current page plus. On's Class)

$ (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 () ADDCLA SS ("on");
return;
}
$ (a2[0]). Parent (). addclass ("on");

CSS section (can be adjusted according to their own 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 with your JavaScript programming.

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: info-contact@alibabacloud.com 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.