jQuery Plugin: Adding Callback functionality

后端 未结 6 1799
孤街浪徒
孤街浪徒 2020-11-28 00:40

I\'m trying to give my plugin callback functionality, and I\'d like for it to operate in a somewhat traditional way:

myPlugin({options}, function() {
    /*          


        
6条回答
  •  旧时难觅i
    2020-11-28 01:23

    Bringing back a blast from the past.

    Worth noting that if you have two arguments passed, for example:

    $.fn.plugin = function(options, callback) { ... };
    

    Then you call the plugin without the options argument but with a callback then you'll run into issues:

    $(selector).plugin(function() {...});
    

    I use this to make it a little more flexible:

    if($.isFunction(options)) { callback = options }
    

提交回复
热议问题