Calling a function inside a jQuery plugin from outside

前端 未结 2 1005
太阳男子
太阳男子 2020-12-13 05:24

I am trying to work out how to call functions within my jQuery plugin from outside the plugin. The code I have tried is not working. I\'m sure I will have to restructure my

2条回答
  •  醉酒成梦
    2020-12-13 06:21

    Take a look at closures.

    Here is a basic example of what a closure looks like in a jQuery plugin.

    $.fn.plugin = function() {
    
        return {
            helloWorld: function() {
                console.log('Hello World!');
            }
        }
    };
    
    // init plugin.
    var test = $('node').plugin();
    
    // call a method from within the plugin outside of the plugin.
    test.helloWorld();
    

    You can see another example at the following jsfiddle.

    http://jsfiddle.net/denniswaltermartinez/DwEFz/

提交回复
热议问题