从jquery中删除last-child

Remove last-child from jquery

本文关键字:last-child 删除 jquery      更新时间:2023-09-26

我有情况,我必须删除所有元素(.bxwrapper),除了最后的bxwrapper,这是从html中包含ul li为此,我已经编写了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());

你应该先clone然后删除它们。

var list = $('ul.bxslider').clone(true, true);
$('.bx-wrapper').remove();

然后将克隆元素附加到body或其他元素。

$('#customBx').html('<div class="bx-viewport" style="width: 100%; overflow: hidden; position: relative; height: 331px;"></div>').find('.bx-viewport').append( + list + );

你也可以试试这个:

var $lastChildren = $('ul.bxslider').parent().parent('.bx-wrapper').html();
$('#customBx').html($lastChildren);

但是在jquery部分下面使用into

$(document).ready(function(){
// Above code
})