remove last append element jquery

2019-01-31 07:18发布

I have the following code:

$(this).children("a:eq(0)").append('<img src="'+ (arrowsvar.down[1]) 
    +'" class="' + (arrowsvar.down[0])
    + '" style="border:0;" />'
);

Now I want to remove last appended element (an image). Please give suggestions.

标签: jquery append
5条回答
闹够了就滚
2楼-- · 2019-01-31 07:50

amazing, thank you very much eugene, that really helped me. I had to remove a table, i used the following code.

$("#mydivId table:last-child").remove()
查看更多
Emotional °昔
3楼-- · 2019-01-31 07:58

You can use the :last-child selector to find the last appended element, then you can remove it:

$('img:last-child', this).remove();
// get the last img element of the childs of 'this'
查看更多
对你真心纯属浪费
4楼-- · 2019-01-31 07:59

As an alternative, for those who likes more programmatic ways and syntax:

$('#container-element img').last().remove();
查看更多
贼婆χ
5楼-- · 2019-01-31 08:03

The code from the original question may generate errors (using "this" with the selector). Try the following approach:

$("#container-element img:last-child").remove()
查看更多
三岁会撩人
6楼-- · 2019-01-31 08:06

try this. Just added .not(':last-child') which excludes the last element in your collection. So you don't have to remove it.

$(this).children("a:eq(0)").not(':last-child').append('<img src="'+ (arrowsvar.down[1]) 
    +'" class="' + (arrowsvar.down[0])
    + '" style="border:0;" />'
);
查看更多
登录 后发表回答