Two simple ways to realize menu highlighting JS class code _javascript Skills

Source: Internet
Author: User
Remember when writing static pages, in order to achieve highlighting are in each page with a different style, ah. Highlighting I think for the Web front-end, is the more commonly used effect, just this time again to use, specially sorted out I wrote two types of highlighting.

In fact, the idea is very simple, the first method is to determine whether the HREF value is contained in the browser's current URL value by traversing the href value of the link group. This method has some limitations, for example, the menu in the IFRAME can not be judged; The second method is more widely used, the realization of the idea is relatively simple, that is, through the judgment click, to load the highlight style.

The first is to determine the current URL value highlighting class code:
Copy Code code as follows:

@Mr. See---Judge URL implementation menu highlighting
function Highurl (menuid,classcur) {
if (!document.getelementbyid) return false;
if (!document.getelementbyid (MENUID)) return false;
if (!document.getelementsbytagname) return false;
var Menuid=document.getelementbyid (MENUID);
var links=menuid.getelementsbytagname ("a");
for (var i=0; i<links.length; i++) {
var menulink=links[i].href;
var currentlink=window.location.href;
if (Currentlink.indexof (menulink)!=-1) {
Links[i].classname=classcur;
}
}
}

Parameter description:

1.MENUID: The ID of the link group;
2.classCur: The style class name when highlighted.
Call Method:

Window.onload=function Highthis () {Highurl ("Youid", "Youhighclass");}
The second click to highlight the current class:
Copy Code code as follows:

@Mr. See---Click Implementation highlighting
function Highonclick (elemid,classcur) {
if (!document.getelementsbytagname) return false;
if (!document.getelementbyid) return false;
if (!document.getelementbyid (Elemid)) return false;
var elemid = document.getElementById (elemid);
var links = elemid.getelementsbytagname ("a");
for (i = 0; i < links.length; i++) {
Links[i].onclick = function () {
for (n = 0; n < links.length; n++) {
Links[n].classname = "";
This.classname = Classcur;
This.blur ();
}
}
}
}

Parameter description:

1.ELEMID: The ID of the link group;
2.classCur: The style class name displayed after the click.
Call Method:

Window.onload=function Highthis () {Highonclick ("Youid", "Youhighclass");}
This method is more extensible, for example, by judging the Parentnode.nodename value to make a type of link not traversed, and so on.
SOURCE Download and Demo
Since a friend doesn't know how to use it, I specifically sorted out the pages I wrote earlier about this class, giving a demo page and a download address that a friend needs to be able to view or download.

Package Download Address

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.