JS Implementation menu highlighting JavaScript class code

Source: Internet
Author: User

@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 ();
}
}
}
}

Number of instructions:

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");}

@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 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.

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.