Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有api的Jquery插件_Jquery_Api_Plugins - Fatal编程技术网

带有api的Jquery插件

带有api的Jquery插件,jquery,api,plugins,Jquery,Api,Plugins,如何从插件中检索API变量。根据Jquery编写文档,以下是插件结构: (function( $ ){ var methods = { init : function( options ) { return this.each(function() { var settings = { 'height' : 10, 'width'

如何从插件中检索API变量。根据Jquery编写文档,以下是插件结构:

(function( $ ){

    var methods = {

        init : function( options ) {

            return this.each(function() {
                var settings = {
                    'height' : 10,
                    'width' : 10
                };

                if ( options ) { $.extend( settings, options ); }

                $('<div>', {
                    css : {
                        height : settings.height,
                        width : settings.width,
                    }
                }).appendTo(this);
            });

        }

    };

    $.fn.myPlugin = function( method ) {

        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.element' );
        }    

    };

})( jQuery );
在properties中,我会有一个函数返回Div的高度和宽度。在这个设计中,我将如何创建properties变量


谢谢

你会摆脱

return this.each(function() { ... });
…并让您的插件返回诸如

return {
   'properties': {
       'width': calculatedWidth,
       'height': calculatedHeight
    }
}

然而,这样做将意味着你无法链接其他方法。

你将摆脱

return this.each(function() { ... });
…并让您的插件返回诸如

return {
   'properties': {
       'width': calculatedWidth,
       'height': calculatedHeight
    }
}

但是,这样做意味着你不能链接其他方法。

谢谢,事实上我的插件中有更多的方法。这只是一个例子。理想情况下,我希望只有创建元素的init方法,然后我会像这样执行所有其他操作<代码>变量元素=$('div').myPlugin();元素destroy();元素。getProperties()谢谢,事实上我的插件中有更多的方法。这只是一个例子。理想情况下,我希望只有创建元素的init方法,然后我会像这样执行所有其他操作<代码>变量元素=$('div').myPlugin();元素destroy();元素。getProperties()