我遇到的情况是,我必须删除除最后一个包含 html 中的 ul li 的 bxwrapper 之外的所有元素(.bxwrapper),为此我编写了无法正常工作的 jquery 代码,请提出建议。?
<div class="row" id="customBx">
<div class="bx-wrapper" style="max-width: 100%;">
<div class="bx-viewport" style="width: 100%; overflow: hidden; position: relative; height: 336px;">
<div class="bx-wrapper" style="max-width: 100%;">
<div class="bx-viewport" style="width: 100%; overflow: hidden; position: relative; height: 335px;">
<div class="bx-wrapper" style="max-width: 100%;">
<div class="bx-viewport" style="width: 100%; overflow: hidden; position: relative; height: 333px;">
<div class="bx-wrapper" style="max-width: 100%;">
<div class="bx-viewport" style="width: 100%; overflow: hidden; position: relative; height: 331px;">
<ul class="bxslider">
<li class="edSel edBxSlider editBxSlider edRemove" style="float: left; list-style: none; position: relative; width: 990px;"></li>
<li class="edSel edBxSlider editBxSlider edRemove" style="float: left; list-style: none; position: relative; width: 990px;"></li>
</ul>
</div>
<div class="bx-controls bx-has-controls-direction">
<div class="bx-controls-direction">
<a class="bx-prev" href="">Prev</a><a class="bx-next" href="">Next</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$('.bx-wrapper:not(:last-child)').remove();
</script>
最佳答案
您可以使用detach()
,所以试试这个:
var $lastChild = $('.bx-wrapper').last().detach();
$('#customBx').html($lastChild);
或者简单地
$('#customBx').html($('.bx-wrapper').last());
关于javascript - 从 jquery 中删除最后一个子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30683036/