使用jQuery将所有元素包装在div中 [英] Use jQuery to wrap all elements in a div

查看:101
本文介绍了使用jQuery将所有元素包装在div中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的标记非常奇怪,但是我有3个div:

I have quite a strange markup but nevertheless, i have 3 divs:

<div id="div1"></div>

<div id="div4"></div>

<div id="div12"></div>

这3个div之间可能还有其他元素,但是我需要做的是确保有一个包裹这3个div的div,这样标记最终会像这样:

There may be other elements in between these 3 divs but what i need to do is make sure that there is a div wrapping these 3 divs so the markup will end up like this:

<div class="wrapped">
    <div id="div1"></div>

    <div id="div4"></div>

    <div id="div12"></div>
</div>

是否可以使用jQuery的wrap或wrapAll函数?

Is this possible using jQuery's wrap or wrapAll functions?

推荐答案

wrapAll().您可以使用 nextUntil() add()进行匹配#div1#div12之间的元素(包括):

That's possible with wrapAll(). You can use nextUntil(), andSelf() and add() to match the elements between #div1 and #div12, inclusive:

$("#div1").nextUntil("#div12").andSelf().add("#div12")
          .wrapAll("<div class='wrapped'></div>");

这篇关于使用jQuery将所有元素包装在div中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆