jQuery添加带有条件排列的div元素



我想问一下,如果我们使用 jQuery 添加div元素,是否可以,但我需要这样的条件:

我之前的代码

<li>
    <a href="" class="1"></a>
    <a href="" class="2"></a>
    <div class="another"></div>
    <a href="" class="3"></a>
</li>

我想要的是:

<li>
    <a href="" class="1"></a>
    <div class="new">
        <a href="" class="2"></a>
        <div class="another"></div>
        <a href="" class="3"></a>
    </div>
</li>

如果可能的话,我希望有人可以帮助我。

您可以创建新元素,将其附加到所需的任何位置,然后将所需的元素附加到新元素中:

var wrapper = $('<div class="new"/>'); 
$('li').find('a').first().after(wrapper).nextAll().appendTo(wrapper);
你想要

什么的标准并不完全清楚,但你可以尝试使用wrapAll$(".2, .3, .another").wrapAll("<div class='new' />");

$(".2, .3, .another").wrapAll("<div class='new' />");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<li>
    <a href="" class="1"></a>
    <a href="" class="2"></a>
    <div class="another"></div>
    <a href="" class="3"></a>
</li>

最新更新