我有一组不同的元素,例如:
<section class="box-1"></section>
<section class="box-2"></section>
<section class="box-3"></section>
我想用 div + class 包装所有这 3 个元素 所以它看起来像这样:
<div id="new">
<section class="box-1"></section>
<section class="box-2"></section>
<section class="box-3"></section>
</div>
我尝试了 wrap
, wrapAll
, before
, after
以及 jQuery 中的更多方法,但没有一个有效。
所有这些方法都添加了 <div id="new">
, 但立即关闭 div
像这样:
<div id="new"></div>
<section class="box-1"></section>
<section class="box-2"></section>
<section class="box-3"></section>
</div>
我只需要一种添加 OPEN div
的方法仅在:
<div id="new">
并关闭div
在最后一个元素之后:
</div>
最佳答案
使用wrapAll()
方法
$('.box-1,.box-2,.box-3').wrapAll('<div/>')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="box-1"></section>
<section class="box-2"></section>
<section class="box-3"></section>
关于javascript - Jquery 用打开和关闭标签包装一组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40418066/