Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery将其传递给变量函数_Javascript_Jquery_Jquery Plugins_Anonymous Function - Fatal编程技术网

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
]重命名对
项的所有引用。

您尚未在此处定义变量项