I have an unordered list that I am prepending data to as follows:
jQuery("#mylist").prepend(newItem);
When the list reaches a certain size I need to remove the first item that was inserted before adding the new one.
How would I get the first item to remove based on accessing the ordered list by it's id.
Something like:
jQuery("#mylist")[0].remove();
Thanks
Since you mentioned ordered list, im assuming #mylist contains li tags inside, thus this should work
jQuery("#mylist li:first-child").remove();
I see you are prepending there, In case you want to remove the last element then
jQuery("#mylist li:last-child").remove();
因为你正在做.prepend()
,所以插入的第一个项目将是列表中的最后一项,所以你要这样做:
$("#mylist").children().last().remove();
The [0]
is the same as get(0)
which is selecting the DOM element. The DOM does not have .remove()
. Plus you are selecting the parent element and not the children.
You can use eq(0) to select the first
jQuery("#mylist li").eq(0).remove();
there are other jQuery selectors/methods such as .first(), :first, :first-child
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.