简体   繁体   中英

Call a function in a jquery plugin using an argument

I've a basic jquery plugin where uses data attributes data-something="thing" to tell the plugin what function run inside of it.

I was using the next to do it:

// Plugin definition.
$.fn.plugin = function( options ) {
    // Iterate and reformat each matched element.
    return this.each(function() {
        var func = element.data('something');
        var response = plugin[func]($(this));
    });
};

And it works just fine. But i wanted to follow the jquery standars where functions should be called:

$.fn.plugin.thing($(this));

So...the question. How to archive that? Is that possible when the same of the function comes in text?

Thanks!

If I am interpreting your question correctly, you're looking to be able to use $.fn.plugin.func instead of $.fn.plugin[func] , where func is a variable storing a string that is the method name? If that is the case, I don't think it is possible because JavaScript will be looking for a method named func in the plugin variable as opposed to the method named after the string contained in the func variable.

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