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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13549254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:07:31  来源:igfitidea点击:

How to call function in jquery plugin?

javascriptjqueryhtml

提问by FatDogMark

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

call it in doucument ready in another file

在另一个文件中准备好文件中调用它

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

I thought it should work ,I got an error, what's the problem?

我认为它应该可以工作,但出现错误,有什么问题?

采纳答案by u283863

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);

回答by Rahul Tripathi

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>

回答by pocesar

when using varinside a function, it will make the element "private". it's a hacky way to make visibility in Javascript work, while true class structure don't come to Javascript. You need to either set it to the prototype of your function or to return an object

var函数内部使用时,它将使元素“私有”。这是在 Javascript 中实现可见性的一种hacky 方式,而真正的类结构不会出现在 Javascript 中。您需要将其设置为函数的原型或返回一个对象

(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_islidesobject, but accessing through jQuery, so the first option is your best bet.

但在您的情况下,您不会使用原型,因为您不是实例化新top_islides对象,而是通过 jQuery 访问,因此第一个选项是您最好的选择。

回答by olivier dufour

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/

文档可以在这里找到:http: //api.jquery.com/on/