jQueryプラグインの初体験-(一)

4808 ワード

1.jqueryには2つの拡張方法があります.
  • jquery.fn.extend=jquery.prototype.extend
  • jquery.extend

  • (両者の違いは後述)
    2.具体例構造:
    //       
    (function ($) { //
        var methods = { inint: function (options) { //  “this”(  each()      this),        。
                return this.each(function () { //             jQuery  
                    var $this = $(this); //     
                    var defaults = { proName: 'zqz' }; if (typeof (options) == 'undefined') { settings = $.extend({}, defaults); } else { var settings = { proName: options.proName, Events: handlers.zqzClick($this, options) }; settings = $.extend({}, defaults, settings); } }) } }; //  
            var handlers = { zqzClick: function ($this, options) { $this.bind('click', options.zqzClick); } }; // jQuery     “fn”             , “zqz”         
            $.fn.zqz = function () { //         
                var method = arguments[0]; if (methods[method]) { method = methods[method]; arguments = Array.prototype.slice.call(arguments, 1); //        ,           (JSON  )  method       
            } else if (typeof (method) == 'object' || !method) { method = methods.inint; } else { $.error('asdasd'); return this; } return method.apply(this, arguments); } })(jQuery) //  
    $(function () { //$('#blanckjh').zqz();
               $('#blanckjh').zqz({ 'proName': 'wdx', 'zqzClick': function () { alert("zqz"); } }); })

    3.プラグインに関するいくつかの概念と注意事項は後述の文章に置く.