jQuery 插件子函数中的无限递归
我编写了以下 jQuery 插件:
(function($){
$.fn.imageSlide = function(options){
$(this).imageSlide.nextSlide();
console.log("imageslide");
};
$.fn.imageSlide.nextSlide = function(){
console.log("nextslide");
$this = $(this);
};
})(jQuery);
一些背景:
我想要一个图像滑块插件,用于淡入淡出背景(出于性能原因,我无法使用 超大 插件)。我想向用户公开几个函数:imageSlide插件“构造函数”和其他几个函数,例如imageSlide.nextSlide
和imageSlide.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定到底是什么导致了错误,但没有必要将所有静态方法都放在 jQuery 原型中。
尝试使用以下方法公开插件:
现在您可以调用插件及其方法,如下所示:
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:
Now you can call the plugin and it's methods like this: