2012-02-08 58 views
6

我需要一些帮助,我的jQuery脚本。 我有一个页面,每10秒刷新一次,并且来自feed的新div被附加到。如果有超过20个|,则删除旧的div jQuery

我的脚本对div进行计数,并在有多于20个div时删除最后一个div。如果Feed一次只附加1个div,此工作正常。但Feed也可以同时添加多个div。发生这种情况时,计数可能会超过20个div的最大值。这个问题是我的脚本只删除了1个div而不是所有超过20个count的div。

这是我的代码:

var $auto_refresh = setInterval(function() { 
    var $articleCount = $('div').length; 

    if ($articleCount > 20) { 
     $('div:last-child').remove(); 
    } 

    $autoUpdate(); 
}, 10000); // refresh every 10000 milliseconds for new items 

我需要那么总会有20个div的去除所有多余的div。我希望有人能帮助我解决这个问题。

回答

3
var $auto_refresh = setInterval(function() { 
    var $articleCount = $('div').length; 

    while ($articleCount > 20) { 
     $('div:last-child').remove(); 
     $articleCount = $('div').length; 
    } 

    $autoUpdate(); 
}, 10000); 

通知ifwhile的变化。这使删除最后一个,直到有20

+2

这是行不通的,因为'$ articlecount'不会实际更新过程中'while';) – Supr 2012-02-08 15:15:54

+0

现在,它是肯定的。 – 2012-02-08 15:20:44

+0

对不起,我认为这是[HTMLCollection正在直播](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-75708506)的情况 - 没有注意到你它的长度是一个单独的变量 – 2012-02-08 15:26:44

1

使用greater than选择:

var $auto_refresh = setInterval(function() { 

    $('div:gt(20)').remove(); 

    $autoUpdate(); 
}, 10000); // refresh every 10000 milliseconds for new items 
+0

不需要更新循环体内的'$ articleCount'? – xbonez 2012-02-08 15:16:39

+0

':gt'是一个jQuery过滤器,因此将它用作选择器的一部分将停止使用CSS引擎选择div的jQuery,这意味着性能受到影响。最好使用'$('div')。filter(':gt(20)')'或类似的东西('。slice()') – Joe 2012-02-08 15:18:42

+0

@Joe你认为性能是一个问题吗? – 2012-02-08 15:34:03

0
var $auto_refresh = setInterval(function() { 

    while ($('div').length > 20) { 
     $('div:last-child').remove(); 
    } 

    $autoUpdate(); 
}, 10000); // refresh every 10000 milliseconds for new items 
0
while ($articleCount > 20) {    
     $('div:last-child').remove(); 
     $articleCount = $('div').length; 
    } 
2

你可以使用.slice(x)删除从指数x和所有元素:http://jsfiddle.net/PLKAm/

$("div").slice(20).remove(); 

如果有<= 20项然后.slice(20)返回一个空集,所以代码是一个无操作自动进行。

0

编辑的简单:

$('div').each(function(count){ 
    if(count >= 20){ 
     $(this).remove(); 
    } 
}); 
+0

我不知道为什么这会被拒绝 – 2012-02-08 15:39:23

+0

你可以简化这个:[each function](http://api.jquery.com/each/)有两个参数,第一个是索引,所以你的count var没有必要。 – 2012-02-08 15:40:46

+0

@TimBüthe谢谢,编辑。 – wanovak 2012-02-08 15:45:16

6

使用jQuery.slice让一切过去的数20,和斌他们 - 死很容易:)

var $auto_refresh = setInterval(function() { 
    $('div').slice(20).remove(); 
    $autoUpdate(); 
}, 10000); // refresh every 10000 milliseconds for new items 

http://api.jquery.com/slice/

0

你可以使用:gt()选择器一举找到元素。

var $auto_refresh = setInterval(function() { 
    var nToDelete = $('div').length - 20; // Calculate how many there are to delete...  
    if(nToDelete > 0) $('div:gt(" + nToDelete + ")').remove(); // and delete them. 
    $autoUpdate(); 
}, 10000); 
0
var remove21 = function() { 
    if ($('div').length > 20) { 
     $('div:nth-child(21)').remove(); 
     remove21(); 
    } 
} 

var $auto_refresh = setInterval(function() { 

    remove21(); 

    $autoUpdate(); 
}, 10000); // refresh every 10000 milliseconds for new items