JQuery Tabs Tutorial

Source: Internet
Author: User

Tabs is easy-to-implement and can be built to work with your existing markup. This guide would walk through the process.

Writing the Markup

Lets Start by writing we markup as if our visitor doesn ' t even has JavaScript enabled. Even if you don't wish to the support users without JavaScript, it's still a good pattern to follow to exercise separation O F concerns.

  <Ulclass=' Tabs ' ><Li><Ahref=' #tab1 ' >tab 1</A></Li><Li><Ahref=' #tab2 ' >tab 2</A></Li><Li><Ahref=' #tab3 ' >tab 3</A></Li></Ul><DivId=' Tab1 ' ><p>hi, this is the first tab.</p> Span class= "Hljs-tag" ></div> < Div id= ' tab2 ' > <p>this is the 2nd tab.</p> </div> < div id= ' tab3 ' > <p>and This is the 3rd tab.</p> </DIV>     

I used fragment identifiers ( #tab1 , #tab2 , #tab3 ) for the HREF values in the navigation. Each element containing a tab's content is given an ID, which corresponds to a fragment identifier. This is the links is semantic and continue to being functional even if the visitor has JavaScript disabled.

JavaScript provides direct access to the fragment identifier, or hash, for anchor element objects and the Window.location Object by using the their hash property.

Writing the JQuery

Rather than describing the code, I am just going to include it with comments this explain each step.

 $ (' ul.tabs '). each (function () {//per set of tabs, we want to keep track of//which tab is active and it's ' s    ociated content var $active, $content, $links = $ (this). Find (' a ');    If the Location.hash matches one of the links, use the As the active tab.    If no match is found, use the first link as the initial active tab.    $active = $ ($links. Filter (' [href= ' ' +location.hash+ ' "] ') [0] | | $links [0]);    $active. addclass (' active ');    $content = $ ($active [0].hash);    Hide the remaining content $links. Not ($active). each (function () {$ (This.hash). Hide ();    });      Bind The Click event handler $ (this). On (' Click ', ' a ', function (e)} {//Make the Old tab inactive.      $active. Removeclass (' active ');      $content. Hide ();      Update the variables with the new link and content $active = $ (this);      $content = $ (This.hash);      Make the tab active.      $active. addclass (' active ');      $content. Show (); Prevent The anchor ' s defaultClick action E.preventdefault ();  }); });


See the complete document. I used jquery's. On () method for the event binding, so is sure to the use of jquery 1.7 or higher.

JQuery Tabs Tutorial

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.