简体   繁体   English

如何在jquery插件中调用函数?

[英]How to call function in jquery plugin?

(function($) { 
    $.fn.top_islides = function(){
        var ajax_init = function(){
            init_islides();
            setTimeout(function(){picmove()},300);
        };
//.....
    };  
})(jQuery);

call it in doucument ready in another file 在另一个文件中准备好的doucument中调用它

$('#top_slides').top_islides();
$('#top_slides').top_islides().ajax_init();

I thought it should work ,I got an error, what's the problem? 我认为它应该工作,我得到一个错误,有什么问题?

Do it like this: 像这样做:

(function($) {
    //Assuming $.fn.top_islides is defined
    $.fn.top_islides.ajax_init = function(){
        init_islides();
        setTimeout(picmove,300);
    };
 //.....
})(jQuery);

Or 要么

(function($) { 
    $.fn.top_islides = function(){
        var ajax_init = function(){
            init_islides();
            setTimeout(picmove,300);
        };
        return {
            ajax_init: ajax_init
        };
    });
     //.....
})(jQuery);

Try something like this as in the example below:- 尝试这样的事情,如下例所示: -

<script type="text/javascript">


    $.someplugin = {
      CallMe : function() {
            alert("You called?");
        },
      otherstuff : function() { alert("other stuff!"); }
    };


    $.someplugin.CallMe();
    $.someplugin.otherstuff();
</script>

when using var inside a function, it will make the element "private". 当在函数内部使用var时,它将使元素“私有”。 it's a hacky way to make visibility in Javascript work, while true class structure don't come to Javascript. 这是一种在Javascript工作中实现可见性的hacky方式,而真正的类结构不适用于Javascript。 You need to either set it to the prototype of your function or to return an object 您需要将其设置为函数的原型或返回对象

(function($) { 
  $.fn.top_islides = function(){
    var ajax_init = function(){
        init_islides();
        setTimeout(function(){picmove()},300);
    };
    return {
      'ajax_init': ajax_init
    };
  //.....
  };  
 })(jQuery);

or 要么

(function($) { 
  $.fn.top_islides = function(){
  //.....
  };
  $.fn.top_islides.prototype.ajax_init = function(){
    init_islides();
    setTimeout(function(){picmove()},300);
  }
 })(jQuery);

but in your case, you won't be using prototype, since you aren't instantiating a new top_islides object, but accessing through jQuery, so the first option is your best bet. 但在你的情况下,你不会使用原型,因为你没有实例化一个新的top_islides对象,而是通过jQuery访问,所以第一个选项是你最好的选择。

Imo, best solution is used trigger which is cleaner because you can keep the chainable plugin system. Imo,最好的解决方案是使用触发器,它更干净,因为你可以保持可链接的插件系统。

You can declare event handler in your plugin declaration and trigger from outside: 您可以在插件声明中声明事件处理程序并从外部触发:

(function($) { 
  $.fn.top_islides = function(){
    this.on ('init_islides', function(){
        setTimeout(function(){picmove()},300);
    };
  //.....
  };  
 })(jQuery);
$( "button" ).click(function () {
  $( "p" ).trigger( "init_islides");
});

DOC can be found here : http://api.jquery.com/on/ DOC可以在这里找到: http//api.jquery.com/on/

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

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