38

我有以下代码:

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

现在我想删除最后附加的元素(图像)。请提出建议。

4

5 回答 5

58

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

$('#container-element img').last().remove();
于 2013-03-15T07:26:08.813 回答
36

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

$('img:last-child', this).remove();
// get the last img element of the childs of 'this'
于 2009-10-30T15:28:39.533 回答
20

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

$("#container-element img:last-child").remove()
于 2011-05-21T20:42:34.200 回答
6

太棒了,非常感谢尤金,这真的帮助了我。我不得不删除一个表,我使用了以下代码。

$("#mydivId table:last-child").remove()
于 2012-05-01T06:04:28.210 回答
2

尝试这个。刚刚添加了 .not(':last-child') ,它不包括集合中的最后一个元素。所以你不必删除它。

$(this).children("a:eq(0)").not(':last-child').append('<img src="'+ (arrowsvar.down[1]) 
    +'" class="' + (arrowsvar.down[0])
    + '" style="border:0;" />'
);
于 2011-12-02T05:15:23.760 回答