Two simple implementation of the menu highlighting the JS class (reproduced)

Source: Internet
Author: User

Recently wrote a blog management background front end, involves two kinds of highlighting the current menu on the same page. Remember when the static page, in order to achieve highlighting are in each page with a different style, uh. Highlighting I think for the Web front end, is more commonly used to the effect, just this time again to use, I specifically sorted out the two types of highlighting I wrote.

In fact, the idea is very simple, the first method is to traverse the link Group's href value, through indexof to determine whether the HREF value is included in the browser's current URL value. This method has some limitations, such as the menu within the IFRAME can not be judged; The second method applies to a wider range, the realization of the idea is relatively simple, that is, by judging the click, to click the item load highlight style.

The first to judge the current URL value highlight class code:

@Mr. Think---Determine 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 highlighting.

Call Method:

Window.onload=function Highthis () {Highurl ("Youid", "Youhighclass");}

The second type of click highlights the current class:

@Mr. Think---Click to implement 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 that is displayed after the click.

Call Method:

Window.onload=function Highthis () {Highonclick ("Youid", "Youhighclass");}

This method is more extensible, such as by judging the parentnode.nodename value to make certain types of links not traversed, and so on.

Two simple implementation of the menu highlighting the JS class (reproduced)

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.