Javascript jquery将其传递给变量函数
我正在尝试制作我自己的图像旋转器,如果屏幕上有多个图像旋转器,它就会工作。以下是我到目前为止得到的信息:Javascript jquery将其传递给变量函数,javascript,jquery,jquery-plugins,anonymous-function,Javascript,Jquery,Jquery Plugins,Anonymous Function,我正在尝试制作我自己的图像旋转器,如果屏幕上有多个图像旋转器,它就会工作。以下是我到目前为止得到的信息: // jQuery plugin to make image containers rotate. (function($){ // Swap text with title attribute $.fn.scWFWImageRotator = function() { var rotatorTimeSwap = 6000; $(this
// jQuery plugin to make image containers rotate.
(function($){
// Swap text with title attribute
$.fn.scWFWImageRotator = function() {
var rotatorTimeSwap = 6000;
$(this).find("img").removeClass("selected");
$(this).find("img:first-child").addClass("selected");
var rotatorImageChangeFunc = function(item) {
var rotatorImages = $(item).children("img");
var imgSelected = $(item).children("img.selected");
var rotatorImgCount = rotatorImages.length;
var rotatorCurImage = $(imgSelected).index(rotatorImages);
alert(item);
}
return this.each(function() {
var rotatorTimer;
var $this = $(this);
var func = $.proxy( rotatorImageChangeFunc, $this );
rotatorTimer = setInterval(func, rotatorTimeSwap);
$this.hover(
function() { rotatorTimer = clearInterval(rotatorTimer); },
function() { rotatorTimer = setInterval(func, rotatorTimeSwap); }
);
});
};
})(jQuery);
问题是:
rotatorImageChangeFunc=函数(项){
item没有被传递到函数中。因此在该函数中,我没有为item定义。为什么会出现这种情况,以及如何纠正这种情况?传递到的上下文参数作为this
传递到函数,而不是作为参数。只需将item
更改为this
旁注:在主函数中,您有两个
$(this)。查找(…)
s。插件函数看到的this
已经是一个jqueryt对象(这就是为什么您的this。下面的每个(…)
都可以工作的原因),无需再次对其调用$()
。只需this。查找(…)
$。代理仅为包装的函数设置此
——它不处理函数参数
setInterval()
将不带参数地调用代理函数,因此原始的-项将未定义
要修复,请从函数声明中删除项
,然后执行以下操作:
var item = this;
在函数的第一行中
[或使用this
]重命名对项的所有引用。您尚未在此处定义变量项