remove last append element jquery

孤人 提交于 2020-02-12 07:29:46

问题


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.


回答1:


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

$('#container-element img').last().remove();



回答2:


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'



回答3:


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

$("#container-element img:last-child").remove()



回答4:


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()



回答5:


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;" />'
);


来源:https://stackoverflow.com/questions/1650463/remove-last-append-element-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!