Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.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
创建具有多个功能的jquery插件_Jquery_Jquery Plugins - Fatal编程技术网

创建具有多个功能的jquery插件

创建具有多个功能的jquery插件,jquery,jquery-plugins,Jquery,Jquery Plugins,好的,我是一个新手,我在我的项目中使用了很多插件,我还编写了一些基本插件,这些插件只在带有选项的元素上工作: (function($){ $.fn.pulse = function(options) { // Merge passed options with defaults var opts = jQuery.extend({}, jQuery.fn.pulse.defaults, options); return this.each(

好的,我是一个新手,我在我的项目中使用了很多插件,我还编写了一些基本插件,这些插件只在带有选项的元素上工作:

(function($){
    $.fn.pulse = function(options) {
        // Merge passed options with defaults
        var opts = jQuery.extend({}, jQuery.fn.pulse.defaults, options);
        return this.each(function() {
            obj = $(this);             
            for(var i = 0;i<opts.pulses;i++) {
                obj.fadeTo(opts.speed,opts.fadeLow).fadeTo(opts.speed,opts.fadeHigh);
            };
            // Reset to normal
            obj.fadeTo(opts.speed,1);
        });
    };
    // Pulse plugin default options
    jQuery.fn.pulse.defaults = {
        speed: "slow",
        pulses: 2,
        fadeLow: 0.2,
        fadeHigh: 1
    };
})(jQuery); 
原因是我有一个相当大的脚本,它执行各种ajax调用来保存数据和查询数据库中的数据(使用外部php文件)。我想将所有这些功能集成到一个插件中,但我不知道使用它的最佳结构,我看了这么多教程,基本上绞尽脑汁,我不知道该怎么做

这只是创建一个新函数的问题,如:

$.fn.pluginname.dothis = function(options){
        return this.each(function() {
            //execute code
        };
   };
任何关于这方面的建议,或者一个让我开始学习的模板都会非常有帮助

永远需要帮助


下一个问题:

(function($){
// Can use $ without fear of conflicts
    //var gmap3plugin = $.fn.gmap3plugin;
    var obj = this; // "this" is the jQuery object

    var methods = {
        init : function(options){
            var lat = $.fn.gmap3plugin.defaults[lat];
            var lng = $.fn.gmap3plugin.defaults[lng];
            alert('init'+' lat:'+lat+' --- lng:'+lng);
        },
        show : function( ) {  },
        hide : function( ) { },
        update : function( content ) { }
    };



    $.fn.gmap3plugin = function(method){
        // Method calling logic
        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.tooltip' );
        };
    };
    $.fn.gmap3plugin.defaults = {
        mapdiv :'#mapdiv',
        region : 'uk',
        lat : 53.4807125,
        lng : -2.2343765
    };
})(jQuery);
它正在运行,并获得正确的传递函数, 但是如何访问$.fn.gmap3plugin.defaults中的值呢 init方法中的代码返回未定义的lat和lng

init : function(options){
    var lat = $.fn.gmap3plugin.defaults[lat];
    var lng = $.fn.gmap3plugin.defaults[lng];
    alert('init'+' lat:'+lat+' --- lng:'+lng);
},

或者我不能从一个函数访问$.fn.gmap3plugin.defaults中的数据吗?

如果你看看其他一些jQuery插件和jQuery UI的设计,它们所做的是它们有一个单独的函数
$('#div').myplugin({options})
,然后它们可以通过传递一个字符串而不是一个对象
$('#div')来执行不同的函数.myplugin('performdifferenttask')
,它可以调用对用户隐藏的帮助函数

例如,请看

下面是一个有望缓解您困惑的示例:

(function($) {
    $.fn.myplugin = function(options) {
        if(options == 'function1')
            function1();
        else if(options == 'function2')
            function2();
        else {
            //do default action
        }
    }

    function function1() {
        //do something
    }

    function function2() {
        //do something else
    }
}
然后在使用中:

$.myplugin({option1: 4, option2: 6}); //does default behavior
$.myplugin('function1'); //calls function1()
$.myplugin('function2'); //calls function2()

根据Samuel的回答,您还可以使用以下方法避免重复处理函数名:

(function($) {
    $.fn.myplugin = function(options, param) {
        if( typeof(this[options]) === 'function' ) {
            this[options](param);
        }
        // do default action

        this.function1 = function() {
            //do something
        }

        this.function2 = function(param) {
            //do something else (with a parameter)
        }
    }    
}
通过添加
param
变量,可以调用如下函数:

$.myplugin(); // does default behaviour
$.myplugin('function1'); // run function 1
$.myplugin('function2'); // run function 2
$.myplugin('function2',{ option1 : 4, option2 : 6 }); // run function 2 with a parameter object

请参阅演示。

嗯,好的,我认为jqueryui插件架构提到了一些东西,我确实读过,但它只是让我处于混乱状态!!你能给我指点什么好的教程吗?谢谢samuel,我会试试看,它肯定比我读过的其他文章更有意义。我似乎无法得到你给出的示例的有效实现。我一直在“$.myplugin('function1')”行收到一个“Uncaught SyntaxError:Unexpected identifier”;我知道这是很久以前的事了,但是@Chris您可能会收到一个错误,因为代码有语法错误。$。fn.myplugin=。。。需要一个结束分号和代码段的结尾(jQuery);
$.myplugin(); // does default behaviour
$.myplugin('function1'); // run function 1
$.myplugin('function2'); // run function 2
$.myplugin('function2',{ option1 : 4, option2 : 6 }); // run function 2 with a parameter object