【发布时间】:2010-12-11 15:36:09
【问题描述】:
我有以下代码:
$(this).children("a:eq(0)").append('<img src="'+ (arrowsvar.down[1])
+'" class="' + (arrowsvar.down[0])
+ '" style="border:0;" />'
);
现在我想删除最后附加的元素(图像)。请提出建议。
【问题讨论】:
我有以下代码:
$(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选择器找到最后附加的元素,然后你可以remove它:
$('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;" />'
);
【讨论】: