删除最后一个追加元素 jquery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1650463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove last append element jquery
提问by Basharat Ali
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.
现在我想删除最后附加的元素(图像)。请给出建议。
回答by Arthur Kushman
As an alternative, for those who likes more programmatic ways and syntax:
作为替代方案,对于那些喜欢更多编程方式和语法的人:
$('#container-element img').last().remove();
回答by CMS
You can use the :last-childselector to find the last appended element, then you can removeit:
您可以使用:last-child选择器来查找最后一个附加元素,然后您可以将其删除:
$('img:last-child', this).remove();
// get the last img element of the childs of 'this'
回答by eugene
The code from the original question may generate errors (using "this" with the selector). Try the following approach:
原始问题中的代码可能会产生错误(在选择器中使用“this”)。尝试以下方法:
$("#container-element img:last-child").remove()
回答by charles
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()
回答by StopGapDesign
try this. Just added .not(':last-child') which excludes the last element in your collection. So you don't have to remove it.
尝试这个。刚刚添加了 .not(':last-child') ,它排除了集合中的最后一个元素。所以你不必删除它。
$(this).children("a:eq(0)").not(':last-child').append('<img src="'+ (arrowsvar.down[1])
+'" class="' + (arrowsvar.down[0])
+ '" style="border:0;" />'
);