简体   繁体   English

插件初始化后如何在jQuery插件内部调用函数

[英]How to call function inside jquery plugin after plugin is initialised

I', creating my own jQuery plugin which can be assigned to more than one element in document. 我正在创建自己的jQuery插件,该插件可以分配给文档中的多个元素。 I want, on some events, to call a function inside plugin for that particular element, but everything is driving me crazy. 在某些情况下,我想为该特定元素调用插件内部的函数,但是一切都令我发疯。 Plugin, itself, works, but problem with external calling exists. 插件本身可以工作,但是存在外部调用问题。

simple plugin: 简单的插件:

(function ( $ ) {
    $.fn.myPlugin = function (options) {
        var settings = $.extend({
            someOption: ""
        }, options);
        return this.each(function () {
            _init();
            _load();
        });
        function _init() {
            //some action..
        }
        function _load() {
            //some action..
        }
    };
    $.fn.myPlugin.reload = function () {
        _load(); //or this._load();
    }
}( jQuery ));

and in html: 并在html中:

<div id="div1"></div>
<button id="button1">Click to reload</div>
<script>
    $(document).ready(function() {
        var A=$("div1").myPlugin({
            someOption:"someValue"
        });
        $("#button1").click(function(){
            A.reload();
        });
    });
</script>

return is always that _load() is undefined... Any idea will be really appreciated. 返回总是_load()是未定义的...任何想法将不胜感激。

Youre returning before you define the functions, also _load cannot be accessed from the reload function if its not in a higher scope: 您在定义函数之前先返回,如果_load不在更高的范围内,也无法从_load对其进行访问:

(function ( $ ) {
//function definitions:
    function _init() {
        //some action..
    }
    function _load() {
        //some action..
    }


 $.fn.myPlugin = function (options) {
     var settings = $.extend({
        someOption: ""
    }, options);

    return this.each(function () {
        _init();
        _load();
    });

};
$.fn.myPlugin.reload = function () {
    _load(); //
}
}( jQuery ));

Note that it can be accessed like this: 请注意,可以这样访问它:

 $.myPlugin.reload();

Reload is not part of an myPlugin instance. 重新加载不是myPlugin实例的一部分。

If you want to return a custom object for each instance do this: 如果要为每个实例返回一个自定义对象,请执行以下操作:

 (function ( $ ) {
 $.fn.myPlugin = function (options) {
     var settings = $.extend({
        someOption: ""
    }, options);

     //function definitions:
    function _init() {
        //some action..
    }
    function _load() {
        //some action..
    }
    //iterate
    this.each(function () {
        _init();
        _load();
    });

   return {
     reload:_load,
   };
};
}( jQuery ));

Now you can do 现在你可以做

$("test").myPlugin().reload();

A is holding a reference to a jQuery object and as such has all the methods of $.fn . A持有对jQuery对象的引用,因此具有$.fn所有方法。 But your reload is not a method of $.fn but of $.fn.myPlugin. 但是,您的reload不是$ .fn的方法,而是$ .fn.myPlugin的方法。 If you fix this and take Jonas` answer into account then it should work;) 如果您解决此问题并考虑到乔纳斯的回答,那么它应该可以工作;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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