jquery implementation Click to jump, change the clicked element to add the selected effect

Source: Internet
Author: User

<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

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.