删除最后一个追加元素jquery

Bas*_*Ali 32 jquery append

我有以下代码:

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

现在我想删除最后添加的元素(图像).请给出建议.

Art*_*man 44

作为替代方案,对于那些喜欢更多编程方式和语法的人:

$('#container-element img').last().remove();
Run Code Online (Sandbox Code Playgroud)


CMS*_*CMS 36

您可以使用:last-child选择器查找最后添加的元素,然后您可以删除:

$('img:last-child', this).remove();
// get the last img element of the childs of 'this'
Run Code Online (Sandbox Code Playgroud)


eug*_*ene 17

原始问题的代码可能会产生错误(使用选择器"this").尝试以下方法:

$("#container-element img:last-child").remove()
Run Code Online (Sandbox Code Playgroud)


小智 6

太棒了,非常感谢eugene,这对我很有帮助.我不得不删除一个表,我使用了以下代码.

$("#mydivId table:last-child").remove()
Run Code Online (Sandbox Code Playgroud)

  • 这应该是对eugene答案的评论,而不是单独的答案. (2认同)