<Scripttype= "Text/javascript"src= "Http://www.cnblogs.com/jq.js"></Script>. active {color:red;} HTML code<ulID= "TaB2"><Li><ahref= "http://www.cnblogs.com/index.html">Home</a></Li><Li><ahref= "http://www.cnblogs.com/news.html">News</a></Li><Li><ahref= "http://www.cnblogs.com/about.html">About</a></Li><Li><ahref= "http://www.cnblogs.com/job.html">Job</a></Li><Li><ahref= "http://www.cnblogs.com/ad.html">Advertising</a></Li><Li><ahref= "http://www.cnblogs.com/contactus.html">Contact Us</a></Li></ul>//JQ Code $ (function () {
Window.onload=function () {
var $tDiv = $ ("#tab2"), $links = $tDiv. Find ("a"), index = 0,//Default first menu item URL = Location.href.split ('? ') [0].split ('/'). Pop (). tolocalelowercase ();//Take the last file name of the current URL converted to lowercase
if (URL) {//If there is a fetch, it is matched, otherwise the default home page (that is, the value of index points to the one) for (var i= $links. length; i--;) {//traverse the In-connection address in menu if ($links [I].href.tolocalelowercase (). IndexOf (URL)!==-1) { index = i; break; } } }
Links[index].classname = ' active '; $menu. Find ("A:eq (" + Index + ")"). AddClass ("active");
}
})
The clicked event is a link address trigger for hyperlink A, and the main steps to implement are:
1. When the page is loading at the same time to execute the JS script, the main use is window.onload ()
2. Gets the address of the link to the page, which is the last value after removing the parameter with the argument and gets the value
3. Compare the link address of the hyperlink with the click, and see if it is the link address of the hyperlink by IndexOf to get the location.
4. Find the location of the hyperlink to which the link belongs, plus the corresponding class class name
jquery implementation Click to jump, change the clicked element to add the selected effect