Step by step to create a jQuery tabs plugin - 1

本文深入分析了jQuery UI 的统一API 设计存在的问题,如独特语法、复杂性及易错性,并提出采用更类似于Ext JS、qooxdoo 的设计思路。通过提供一个简单的jQuery tabs 插件示例,展示了更直观、易于使用的API 设计。文章旨在推动jQuery UI 的API 设计朝着更加简洁、易懂的方向发展。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Just as the auther of jQuery Tools said:

jQuery UI has a so-called “unified 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 with
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 should be presented in his own instance not the DOM node in jQuery.

I love jQuery, but i think the components based on jQuery should be more like extjs,
qooxdoo.

Maybe it’s time for me to learn how to write a jQuery plugin, and convert it to
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>
    </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 a.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();
    });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值