使用jQuery删除父元素的最后一个子元素

4
我有一个名为“examples”的父div。我想使用jquery删除该父div中的最后一个div。如何实现?
<div class="examples">
  <div class="test1"></div>
  <div class="test2"></div>
</div>

我想删除示例的最后一个子元素,在这种情况下是test2 div。

1
可能是remove last append element jquery的重复问题。 - Climbatize
6个回答

8
您可以使用find方法遍历子元素,使用div:last选择最后一个子元素,并使用.remove方法将其删除。
$(".examples").find("div:last").remove();

4
尝试这个代码: $('div.examples').children().last().remove();

2

试试这个

$('.examples').children().last().remove()

1
具有最合法外观 - CodeToLife

1

0
如果您想在点击内部 div 时删除父 div 的最后一个子元素,请尝试这个方法。

$(this).parent().children().last().remove();


0

尝试使用带有参数1:nth-last-of-type()选择器

$(".examples div:nth-last-of-type(1)").remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="examples">
  <div class="test1">test1</div>
  <div class="test2">test2</div>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接