Step by step to create a jquery tabs Plugin-1

Source: Internet
Author: User

Just as the auther of jquery tools said:

Jquery UI has a so-called "uniied API" which uses the following syntax
For invoking methods:

// Call select method for tabs $ ("Ul. Example"). tabs ("select", 1 );

API methods are called by supplying the method name as a string followed by method arguments. To be honest, I think that this kind API design is fundamentally wrong. It has the following problems:

  1. The syntax is unique to jquery UI and people outside the UI community are not accustomed to it
  2. The syntax is cubersome. for example, if you want to perform method chaining you have to write the following: $ ("ul. example "). tabs ("select", 1 ). tabs ("disable", 2 );
  3. The JavaScript engine cannot see typos. Writing "selecct" does not look
    Like an error to a browser, making it harder to debug.

I dislike the jquery UI's unified API either. There is another article supporting jquery UI's unified API:

With jquery UI, all the Plugins work with jquery and it's philosophy. Working
John resig's supervision and incite. Working together. Returning a seperate API
Has some potential, but not the way it is implimented here.

In my opinion, a component is a collection of nodes, properties, events and methods,
Which shoshould be presented in his own instance not the DOM node in jquery.

I love jquery, but I think the components based on jquery shoshould be more like extjs,
Qooxdoo.

Maybe it's time for me to learn how to write a jquery plugin, and convert it
The Way Used in jquery tools.

A simple jquery tabs plugin

HTML markup is the same as jquery UI tabs.

<Div class = "tabs"> <ul> <li> <a href = "javascript :; "> Tab 1 </a> </LI> <li> <a href =" javascript :; "> Tab 2 </a> </LI> <li> <a href =" javascript :; "> tab 3 </a> </LI> </ul> <div> pane 1 content </div> <div> pane 2 content </div> <div> pane 3 content </div>

Let's write some basic CSS rules:

 
. Tabs ul {list-style-type: none; padding: 0px; margin: 0px ;}. tabs Li {display: inline ;}. tabs Li a {text-Decoration: none ;}. tabs. current {background-color: # CCC ;}

And the jquery plugin code:

(Function ($) {$. FN. tabs = function () {var tabs = This. children ("Ul "). find ("LI> A"); var panes = This. children ("Div"); var current = 0; function clicktab (INDEX) {current = index; tabs. removeclass ("current "). eq (current ). addclass ("current"); panes. hide (). eq (current ). show () ;}clicktab (0); tabs. click (function () {clicktab (tabs. index (this) ;}); return this ;}) (jquery );

The invoke code:

 
$ (Function () {$ ("Div. Tabs"). tabs ();});

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.