jQuery 插件子函数中的无限递归

发布于 2024-09-11 04:42:23 字数 947 浏览 8 评论 0原文

我编写了以下 jQuery 插件:

(function($){
    $.fn.imageSlide = function(options){
      $(this).imageSlide.nextSlide();
      console.log("imageslide");
    };

    $.fn.imageSlide.nextSlide = function(){
      console.log("nextslide");
      $this = $(this);
    };

})(jQuery);

一些背景:

我想要一个图像滑块插件,用于淡入淡出背景(出于性能原因,我无法使用 超大 插件)。我想向用户公开几个函数:imageSlide插件“构造函数”和其他几个函数,例如imageSlide.nextSlideimageSlide.previousSlide,以使用户能够执行这些操作来自插件外部。

imageSlide 函数需要调用 imageSlide.nextSlide 函数,以滑入(或淡入)第一个图像。

问题:

似乎行 $this = $(this); 触发了 imageSlide.nextSlide 函数的无限递归。

  • 为什么会发生这种情况?
  • 似乎 $.fn.imageSlide.nextSlide = function(){}; 不是在 jQuery 插件中公开另一个函数的正确方法。我该怎么做?

I wrote the following jQuery plugin:

(function($){
    $.fn.imageSlide = function(options){
      $(this).imageSlide.nextSlide();
      console.log("imageslide");
    };

    $.fn.imageSlide.nextSlide = function(){
      console.log("nextslide");
      $this = $(this);
    };

})(jQuery);

Some background:

I want an image slider plugin, to crossfade backgrounds (for performance reasons I cannot use the Supersized plugin). I want to expose several functions to the user: the imageSlide plugin "constructor" and several other functions, e.g. imageSlide.nextSlide and imageSlide.previousSlide, to enable the user to perform these actions from outside the plugin.

The imageSlide function needs to call the imageSlide.nextSlide function, to slide in (or fade in) the first image.

Problem:

It seems that the line $this = $(this); triggers an infinite recursion of the imageSlide.nextSlide function.

  • Why is this happening?
  • It seems that $.fn.imageSlide.nextSlide = function(){}; is not the right way to expose another function in a jQuery plugin. How am I supposed to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花易冷人易散 2024-09-18 04:42:23

我不确定到底是什么导致了错误,但没有必要将所有静态方法都放在 jQuery 原型中。

尝试使用以下方法公开插件:

(function($) {

// The constructor of your plugin:
var imageSlide = function(elems, options) {
    this.elems = elems; // the targets from the jQuery selector
    this.options = options;
};

// the public inherited methods:
imageSlide.prototype = {
    nextSlide: function() {
        console.log('nextSlide called');
    }
};

// extending the jQuery prototype and returning only one instance:
$.fn.imageSlide = function(options) {
    return new imageSlide(this, options);
};

})(jQuery);

现在您可以调用插件及其方法,如下所示:

var myGallery = $('#gallery').imageSlide();
myGallery.nextSlide();

I'm not sure exactly what is causing the error, but there is no need to put all your static methods in the jQuery prototype.

Try exposing the plugin using something like:

(function($) {

// The constructor of your plugin:
var imageSlide = function(elems, options) {
    this.elems = elems; // the targets from the jQuery selector
    this.options = options;
};

// the public inherited methods:
imageSlide.prototype = {
    nextSlide: function() {
        console.log('nextSlide called');
    }
};

// extending the jQuery prototype and returning only one instance:
$.fn.imageSlide = function(options) {
    return new imageSlide(this, options);
};

})(jQuery);

Now you can call the plugin and it's methods like this:

var myGallery = $('#gallery').imageSlide();
myGallery.nextSlide();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文