我有以下代码:
$(this).children("a:eq(0)").append('<img src="'+ (arrowsvar.down[1])
+'" class="' + (arrowsvar.down[0])
+ '" style="border:0;" />'
);
现在我想删除最后附加的元素(图像)。请提出建议。
作为替代方案,对于那些喜欢更多编程方式和语法的人:
$('#container-element img').last().remove();
您可以使用:last-child选择器查找最后附加的元素,然后可以将其删除:
$('img:last-child', this).remove();
// get the last img element of the childs of 'this'
原始问题中的代码可能会产生错误(在选择器中使用“this”)。尝试以下方法:
$("#container-element img:last-child").remove()
太棒了,非常感谢尤金,这真的帮助了我。我不得不删除一个表,我使用了以下代码。
$("#mydivId table:last-child").remove()
尝试这个。刚刚添加了 .not(':last-child') ,它不包括集合中的最后一个元素。所以你不必删除它。
$(this).children("a:eq(0)").not(':last-child').append('<img src="'+ (arrowsvar.down[1])
+'" class="' + (arrowsvar.down[0])
+ '" style="border:0;" />'
);