简体   繁体   中英

Pass jQuery plugin as a argument to function.

I am looking for advice. What is the best method to pass jQuery plugin as a argument to function and execute it in context of DOM element. I want to do something like this:

(function ($) {
    $.fn.firstPlugin = function (options) {

    };
})(jQuery);


(function ($) {
    $.fn.secondPlugin = function (options) {

    };
})(jQuery);


function someFunction(argumentPlugin) {
    var someElement = document.getElementById("something");
    argumentPlugin.call(someElement); 
}

// what is better ?  
someFunction($.fn.firstPlugin);
// or
someFunction($.firstPlugin);

I would appreciate any ideas and tips. Best regars.

What if you just passed in the name of the plugin

Say the name is firstPlugin and is used like this $(element).firstPlugin();

So just do ...

function someFunction(pluginName) {
    var someElement = document.getElementById("something");
    $(someElement)[pluginName](); 
}

someFunction('firstPlugin');

$.firstPlugin will be undefined. $.fn is nothing but $.prototype so you cannot find the function reference in $.firstPlugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM