简体   繁体   English

如何在jQuery插件中外部调用方法?

[英]How do I externally call a method within a jQuery plugin?

Is there a way to call a method defined in a jQuery plugin directly in my script? 有没有一种方法可以直接在我的脚本中调用jQuery插件中定义的方法?

update 更新

(function($){
  $.fn.myPlugin = function (options) {   
    return this.each(function() {

      function doSomething() {
        // do something
      }

      function doSomethingElse() {
        // do something else
      }

      doSomething();
      doSomethingElse();
    });
  }
})(window.jQuery);

now i want to call doSomethingElse() from my script. 现在我想从脚本中调用doSomethingElse()。 Is there a way to do this? 有没有办法做到这一点?

The direct answer to your question is, "no". 您问题的直接答案是“否”。 As posted, that code explicitly hides those functions from the outside world. 如前所述,该代码显式地将这些函数隐藏在外部世界中。

One jQuery-ish way to make those functions available is to define them differently. 使这些功能可用的一种类似于jQuery的方法是对它们进行不同的定义。 Personally, I like to create plugins that have a corresponding "global" object, so if I have 就个人而言,我喜欢创建具有相应“全局”对象的插件,因此如果我有

$.fn.pointy = function() {
  // plugin
}

I'll also have: 我还将有:

$.pointy = {
  something: function() { ... },
  somethingElse: function() { ... }
}

That's nice for providing both relevant utilities and also configuration APIs. 同时提供相关实用程序和配置API很好。 Thus you could put your "doSomething" functions there, and then use them inside your plugin by qualifying the names as $.myPlugin.doSomething() . 因此,您可以将“ doSomething”函数放在此处,然后通过将名称限定为$.myPlugin.doSomething()来在插件中使用它们。 Of course you can also create the functions inside your plugin initialization, so that they might be closures with access to other private functions. 当然,您也可以在插件初始化内创建函数,以便它们可能是可以访问其他私有函数的闭包。

In my opinion best is to use event system when you nedd to call it from outside. 我认为最好是在需要从外部调用时使用事件系统。 Here is the result with your code: 这是您的代码的结果:

(function($){
  $.fn.myPlugin = function (options) {   
    return this.each(function() {
      var $this = $(this);
      $this.on('doSomething', function() {
        // do something
      }

      $this.on('doSomethingElse', function() {
        // do something else
      }

      $this.trigger('doSomethingElse');
      $this.trigger('doSomething');
    });
  }
})(window.jQuery);

or call it from outisde with same method: 或使用相同的方法从outisde中调用它:

$('.myPluginDiv').trigger('doSomethingElse');
$('.myPluginDiv').trigger('doSomething');

You can also use events so the plugin is listening for an event with bind and then you call trigger. 您还可以使用事件,以便插件监听具有bind的事件,然后调用触发器。 Could be a target like window etc. 可能是目标,例如窗口等。

I'm not a 100% sure i get you, but after defining a new function you can still cal it outside of the jQuery Selection, see: 我不是100%肯定会得到您,但是在定义新函数后,您仍然可以在jQuery Selection之外进行校准,请参阅:

 $.fn.myfunc = function(){
   alert('foo');
 }
 $.fn.myfunc(); // works
 $('#something').myfunc(); // works too

So you would have to find out exactly how the methods needs to be called and then do it via $.fn.func() 因此,您必须确切地找到需要如何调用方法,然后通过$.fn.func()

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

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