繁体   English   中英

使用jQuery插件移动元素-setInterval

[英]Move element with jQuery plugin - setInterval

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

(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));

使用setInterval:
http://jsfiddle.net/OmidJackson/46UNg/
没有setInterval:
http://jsfiddle.net/OmidJackson/6bKWU/

你的问题是, this有setTimeout函数字面内不同的含义(window对象)。 在不同的上下文中,请查看此答案以获取有关this信息的更多信息。

解决方案是保存this的引用,以便您可以在setTimeout中使用它。
请参阅此示例

您需要存储this因为它当前具有window的值

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM