JavaScript Learning Note 2 tab Toggle

Source: Internet
Author: User

1, Tab switch abbreviated version 1

The page layout is as follows:

1 <div id= "tab" > 2      

3 <span class= "select" > Title one </span> 4 <span> title two </span> 5 <span> Title III </span> 6 7 <ul id= "Content" > 8 <li class= "Show" > Contents one </li > 9 <li> content two </li> <li> content three </li> </ul> </div>

About the Click event Style is as follows:

1 /* Default Style & post-click Style */ 2 #title. select{3    color: #FFFFFF; 4     background: #0AA770; 5 }6#content. show{7    display:block; 8 }

The default style is as follows:

JS Code:

1  varTit=document.getelementbyid (' title ');2   varCon=document.getelementbyid (' content ');3   varSpans=tit.getelementsbytagname (' span ');4   varLis=con.getelementsbytagname (' Li ');5   //The first for loop gives each span binding a point -and-click event6    for(vari = 0, len= spans.length; i < Len; i++){7spans[i].onclick=function(){8          //The second for Loop traversal finds the current click Yes span9           for(varj = 0,len= Spans.length; J < Len; J + +) {Ten                 if(spans[j]== This) { One                      This. classname= ' SELECT '; A                     //spans[v].classname= ' select ';//effect Ibid. -Lis[j].classname= ' Show '; -}Else{//if it is not selected, empty settings theSpans[j].classname= "; -Lis[j].classname= "; -                 } -          } +       } -}

2, Tab switch abbreviated version 2

Page layout and default style settings are the same as above

JS Code:

1 varTit=document.getelementbyid (' title ');2   varCon=document.getelementbyid (' content ');3   varSpans=tit.getelementsbytagname (' span ');4   varLis=con.getelementsbytagname (' Li ');5   //The first for loop gives each span binding a point -and-click event6    for(vari = 0, len= spans.length; i < Len; i++){7       spans[i].index=i; // different : Gets the index value of the current click event to set the binding object to use later8spans[i].onclick=function(){9          //The second for Loop traversal finds the current click Yes spanTen          // empty all bound objects One           for(varj = 0,len = Spans.length; J < len;j++) { ASpans[j].classname= "; -Lis[j].classname= "; -          } the          //the object is bound to the Click event individually, and the index value is derived from the previously acquired -           This. classname= ' SELECT '; -lis[ This. Index].classname= ' Show '; -       } +}

JavaScript Learning Note 2 tab Toggle

Related Article

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.