Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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插件移动元素-setInterval_Javascript_Jquery_Jquery Plugins - Fatal编程技术网

Javascript 使用jQuery插件移动元素-setInterval

Javascript 使用jQuery插件移动元素-setInterval,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,为什么此代码不起作用? 我写了这段代码,但是: (function($){ $.fn.newsSlider = function() { setTimeout(function() { this.each( function() { $(this).each(function(){ $(this).append($(this).children(":first-child").clone());

为什么此代码不起作用?
我写了这段代码,但是:

(function($){
$.fn.newsSlider = function() {
    setTimeout(function() {
        this.each( function() {
            $(this).each(function(){
                $(this).append($(this).children(":first-child").clone());
                $(this).children(":first-child").remove();
            });
        });
    }, 3000);
}
}(jQuery));

带设置间隔:

无设置间隔:

您的问题是,
在setTimeout函数文本中具有不同的含义(窗口对象)。在不同的上下文中检查有关此的更多信息

解决方案是保存对该的引用,以便您可以在setTimeout内使用它。

请参阅。

您需要存储
,因为它当前的值为
窗口

var $this = this;
setTimeout(function() {
    $this.each( function() {
        $(this).each(function(){
            $(this).append($(this).children(":first-child").clone());
            $(this).children(":first-child").remove();
        });
    });
}, 3000);

你真的清楚你在代码中使用的每一个
$(这个)
吗?请使用JSFIDLE。很抱歉,这个问题超出了setInterval的范围。现在我知道了