简体   繁体   中英

Remove :first-child from sidebar of titles when scrolling

I've wrote a script that appends a div to a sidebar when an article is scrolled to on the page, the div simply contains the title of the article that is loaded in. This is a part of an infinite scroll functionality that I have been building for a news website.

The issue I have ran into during testing is; when the user scrolls to an article quite far down the page, my sidebar begins to contain a lot of titles, so I'm looking to eliminate the :first-child when the sidebar contains 10 child divs (titles).

jQuery:

if(($('#sideArticles').children().size()) > 10){
var del = $('#sideArticles').find(':first-child');
$('#sideArticles').remove(del);
}

Nothing is happening, nor am I receiving any errors from the console. If anyone could shed some knowledge that'd be much appreciated.

Cheers, Rich.

I would try this:

if(($('#sideArticles').children().size()) > 10){
    var del = $('#sideArticles').children().eq(0);
    del.remove();
}

There is not enough code to really do testes but this could work :

if(($('#sideArticles').children().size()) > 10){
    var del = $('#sideArticles').children().eq(0);
    del.remove();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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