开发者

jQuery .remove() and last-child not working as expected

I've got this code whi开发者_Python百科ch is working ok. The new piece of data is coming in ok but when the number of p's gets to 3, nothing happens. The last item doesnt get removed and no new items get appended.

Any ideas?

setInterval(function() {
              $.post(
                'json.php', 
                function(data){
                    $('#tweetBox').append('<p>' + data + '</p>');
                    var list = $('#tweetBox p').length;
                    if (list > 3){
                        $('#tweetBox p:last-child').remove();
                    }               
                }
            );
        }, 5000);


The last item doesn't get removed and no new items get appended.

That indicates that the new item gets appended but removed instantly. You want to reverse the order:

var list = $('#tweetBox p').length;
if (list === 3){
    $('#tweetBox p:last-child').remove();
}
$('#tweetBox').append('<p>' + data + '</p>');


$('#tweetBox p:gt(3)').remove();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜