admin管理员组文章数量:1405412
How do I remove all class divs except the last 2 using jquery?
I have a set of ments that show in a ment_container class. When show all ments is clicked a list of all ments for a particular micropost is shown before the most 2 recent which show by default.
How ever if a user types a ment before clicking to see all ments this ment is added to the list and now 3 recent ments show. Because of the way I've set up some back end code it causes the least most recent ment to be shown twice when the show all link is clicked.
No need to get into the details but what I've decided to do is count the total divs and from the total remove all the divs but 2 (the last 2 in the list which are the most recent).
How can I achieve this using jquery?
Kind regards
How do I remove all class divs except the last 2 using jquery?
I have a set of ments that show in a .ment_container class. When show all ments is clicked a list of all ments for a particular micropost is shown before the most 2 recent which show by default.
How ever if a user types a ment before clicking to see all ments this ment is added to the list and now 3 recent ments show. Because of the way I've set up some back end code it causes the least most recent ment to be shown twice when the show all link is clicked.
No need to get into the details but what I've decided to do is count the total divs and from the total remove all the divs but 2 (the last 2 in the list which are the most recent).
How can I achieve this using jquery?
Kind regards
Share Improve this question asked Apr 18, 2012 at 12:31 LondonGuyLondonGuy 11.1k12 gold badges82 silver badges160 bronze badges2 Answers
Reset to default 7Use slice()
(and credit to @Felix Kling for the update):
$(".ment_container").slice(0, -2).remove();
However, given your description of your problem this doesn't sound like the best course of action. It may be worth you starting a new question about the problem you have with the ments with some detail about your code.
More info on slice().
Have the two last div's that you dont want to include have ID's?
I believe you could use jQuery .not()
to remove the last two div's after you've chosen you're selector something like this
$('.ment_container').not('#lastDiv, #lastButone')
Then you can show all ments but not those 2.
more info on .not()
本文标签: javascriptHow do I remove all class divs except the last 2 using jqueryStack Overflow
版权声明:本文标题:javascript - How do I remove all class divs except the last 2 using jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744886795a2630535.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论