Jquery encapsulation tab

Source: Internet
Author: User

Tab tabs are common on the Internet. They can be seen everywhere on websites, especially on large websites, however, the effect of writing a tab may be different. Some are written in YUI using KISSY. Some are written in js of the original review. Others are written in the Jquery framework. But here I use Jquery. in js, I used children () in Jquery () this method is used to obtain the direct child element in the parent class. Remember that the label in the div of the child element must be obtained. For example, $ ("parent class") is obtained in this way "). children ("div a"); only div labels can be obtained if you want to obtain the tag !! I used to encounter this situation when I made a page! In fact, it is very simple to use Jquery to encapsulate the tab! Just a few lines of code! Below is my HTML structure and CSS

 

 
 
  1. <Style type = "text/css">
  2. Ul, li, div {margin: 0; padding: 0 ;}
  3. Ul, li {list-style: none ;}
  4. . TabWrap {width: pixel PX; margin: 0 auto 50px; overflow: hidden ;}
  5. . Tab {background: # F93; overflow: hidden; width: pixel ;}
  6. . Tab li {float: left; width: 150px; color: # fff; text-align: center; cursor: pointer; height: 22px; line-height: 22px ;}
  7. . Tab li. on {background: # F60 ;}
  8. . TabContent. p-content {padding: 20px; border: 1px solid # ddd; border-top: 0 none ;}
  9. . TabContent. hide {display: none ;}
  10. </Style>
  11. <Script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"> </script>
  12. </Head>
  13. <Body>
  14. <Div class = "tabWrap">
  15. <Ul class = "tab">
  16. <Li> Tab 1 </li>
  17. <Li> Tab 2 </li>
  18. <Li> tab 3 </li>
  19. </Ul>
  20. <Div class = "tabContent">
  21. <Div class = "p-content"> 1111 </div>
  22. <Div class = "p-content hide"> 222 </div>
  23. <Div class = "p-content hide"> 3333 </div>
  24. </Div>
  25. </Div>
  26. <Br/>
  27. <Br/>
  28. <Div class = "tabWrap">
  29. <Ul class = "tab">
  30. <Li> Tab 1 </li>
  31. <Li> Tab 2 </li>
  32. <Li> tab 3 </li>
  33. </Ul>
  34. <Div class = "tabContent">
  35. <Div class = "p-content"> 1111 </div>
  36. <Div class = "p-content hide"> 222 </div>
  37. <Div class = "p-content hide"> 3333 </div>
  38. </Div>
  39. </Div>

Here, HTML uses two identical structures and styles to test whether multiple calls of js on a page are successful! The JS Code is as follows:
 

 
 
  1. <script type="text/javascript">  
  2.     $(function(){  
  3.         function tabs(tabMenu,on,tabContent){  
  4.         $(tabContent).each(function(){  
  5.             $(this).children().eq(0).show();      
  6.         });   
  7.         $(tabMenu).each(function(){  
  8.             $(this).children().eq(0).addClass(on);    
  9.         });  
  10.         $(tabMenu).children().hover(function(){  
  11.             $(this).addClass(on).siblings().removeClass(on);  
  12.             var index = $(tabMenu).children().index(this);  
  13.             $(tabContent).children().eq(index).show().siblings().hide();      
  14.         });  
  15.     }  
  16.     tabs(".tab","on",".tabContent");  
  17. })  
  18.           
  19.     </script> 

Just reference the above js! For more clarity, you can download the source files in the attachment at any time! Let's talk about the js idea for writing this page!

One function tabs (tabMenu, on, tabContent) {} uses this function to encapsulate the method of js calling is tabs (". tab "," on ",". tabContent "), where tabMenu refers to the outer ul on of li, which refers to the outer content of the content that li wants to have when the mouse moves up!

Second, use each to traverse and find the first child element li to add a class on the first content for them to display! Then, when the mouse moves to any li, let the current li Add a class on peer element to delete the class on, and then index the current li to see the number of final content. If his index value and li if the index value is the same, the peer element of the content is hidden! The idea is that simple !!!!

This article from the "tu genhua front-end blog" blog, please be sure to keep this source http://tugenhua.blog.51cto.com/3912301/715331

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.