jQuery添加条件排列的div元素

时间:2018-04-12 17:53:51

标签: javascript jquery html jquery-selectors

我想问一下,如果我们在jQuery中添加1970-01-02元素是可能的,但我需要这样的条件:

我之前的代码

div

我想要的是:

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

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

使用append功能,然后使用remove

$("li").append("<div class=\"new\"></div>"); // append the new <div> element
$("div.new").append($(".2, .3")); // append a copy of the 2 <a> elements to the new <div> element
$("li > .2, li > .3").remove(); // remove the original 2 <a> elements

答案 2 :(得分:0)

您想要的标准并不完全清楚,但您可以尝试使用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>