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的范围。现在我知道了