简体   繁体   English

jQuery插件公共方法应用于多个元素时不起作用

[英]jQuery Plugin public methods not working when applied to multiple elements

I posted a similar issue earlier, but it was flagged as a duplicate. 我之前发布了类似的问题,但将其标记为重复。 However, this referenced article did not answer my question, so I'll try this again, this time using the solution of said article in my example. 但是, 这篇引用的文章没有回答我的问题,因此我将在我的示例中使用上述文章的解决方案再试一次。

The solution provided in this article creates the same issue I had before: when there is more than one element, I cannot call any of the public methods of the plugin. 本文提供的解决方案会产生与我以前相同的问题:当有多个元素时,我无法调用该插件的任何公共方法。

Since no working example was provided, let's start with the code the article gave: 由于没有提供工作示例,因此让我们从文章提供的代码开始:

(function($){

     $.fn.myPlugin = function(options) {
          // support multiple elements
          if (this.length > 1){
              this.each(function() { $(this).myPlugin(options) });
              return this;
          }

          // private variables
          var pOne = '';
          var pTwo = '';
          // ...

          // private methods
          var foo = function() {
              // do something ...
          }
          // ...

          // public methods        
          this.initialize = function() {
              // do something ...
              return this;
          };

          this.bar = function() {
              // do something ...
          };
          return this.initialize();
      }
 })(jQuery);

I LOVE the internal loop so that it's applied to each instance of the element, but I feel the repeated "return this" is redundant. 我喜欢内部循环,以便将其应用于元素的每个实例,但是我觉得重复的“返回此”是多余的。 I think if we removed every single one of them, this plugin would work exactly the same. 我认为,如果我们删除其中的每个插件,那么该插件的工作原理将完全相同。 But, for the sake of argument, I'm going to leave them in my working example. 但是,为了争辩,我将把它们留在我的工作示例中。

As you can see in this jsfiddle example , it works fine when there is only one element. 正如您在此jsfiddle示例中看到的那样 ,当只有一个元素时,它可以正常工作。 The public method runs fine. 公共方法运行良好。

However, if I were to comment the other 4 elements back in like here , it throws an error in the console: "undefined is not a function". 但是,如果要像在这里一样注释其他4个元素,则会在控制台中引发错误:“未定义不是函数”。 This, of course, makes sense since I'm attempting to run the public method on a reference to all elements on not an individual element. 当然,这是有道理的,因为我试图在对单个元素上的所有元素的引用上运行public方法。

Well, then I use .eq(0) to run the method only on the first instance of the element here , but I get the exact same error in the console. 好吧,然后我使用.eq(0)仅在此处的元素的第一个实例上运行该方法,但是在控制台中却得到了完全相同的错误。

So, why isn't calling the public method on the individual element working? 那么,为什么在单个元素上调用public方法不起作用? Is this a scoping issue? 这是范围问题吗?

Please advise. 请指教。 Thanks! 谢谢!

Ok, so I think I've answered my own question. 好的,我想我已经回答了我自己的问题。 The issue is that I'm not applying a jQuery plugin to a DOM element. 问题是我没有将jQuery插件应用于DOM元素。 I'm applying it to a jQuery element. 我将其应用于jQuery元素。 So, if I were to apply the jQuery plugin to a jQuery element, referenced like $element or $('.element'), I can then run any public methods because the scope is the same. 因此,如果我将jQuery插件应用于像$ element或$('。element')之类的jQuery元素,则由于范围相同,因此可以运行任何公共方法。 But, if I were to reference it in a different way, like say $parentelement.eq(0), I'm using a difference reference, one that did not get the plugin applied to it, so naturally, it would not have the defined method. 但是,如果我要以不同的方式引用它,例如$ parentelement.eq(0),则我使用的是差异引用,该引用没有将插件应用到它,因此自然地,它不会具有定义的方法。 I think I'm getting the concept right. 我认为我的想法正确。 Still a little shaky on this. 仍然有点动摇。 Maybe someone else can explain it better. 也许其他人可以更好地解释它。

Nevertheless, while the above code does technically work, public methods are not practical on a jQuery plugin. 尽管如此,尽管以上代码在技术上可以正常工作,但公共方法在jQuery插件上并不实际。 I suggest instead using a Custom Event to make the jQuery plugin do something. 我建议改为使用自定义事件来使jQuery插件执行某些操作。 Like this: 像这样:

 (function($) {
     $.fn.extend({
         myTestPlugin: function() {

             if (this.length > 1) {
                 this.each(function() { $(this).myTestPlugin(); });
             }

             this.done = function() {
                 $(this).html('Done!');
             };

             var alsoDone = function() {
                 $(this).html('Also done!');
             };

             this.html('Replace me!');
             this.on('alsoDone', alsoDone);
         }
     });
 })(jQuery);

Here is an example where I am using trigger to make the plugin do something on an individual element, which works, but the method still fails as expected. 是一个示例,其中我使用触发器使插件在单个元素上执行某些操作,此方法可以正常工作,但该方法仍会按预期失败。

I hope this helps other people with similar issues. 希望这对其他有类似问题的人有所帮助。

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

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