jQuery将元素集合封装在div中

时间:2022-11-28 09:31:23

Hello I would like to use jQuery to wrap sets of elements in a div

你好,我想使用jQuery在div中包装元素集

HTML:

HTML:

<h3>Title</h3>
<ul>
<li>Feature</li>
<li>Feature</li>
</ul>

<h3>Title</h3>
<ul>
<li>Feature</li>
<li>Feature</li>
</ul>

<h3>Title</h3>
<ul>
<li>Feature</li>
<li>Feature</li>
</ul>

Desired Result:

预期的结果:

<div class="box">
    <h3>Title</h3>
   <ul>
    <li>Feature</li>
    <li>Feature</li>
    </ul>
    </div>

<div class="box">
    <h3>Title</h3>
   <ul>
    <li>Feature</li>
    <li>Feature</li>
    </ul>
    </div>

<div class="box">
    <h3>Title</h3>
   <ul>
    <li>Feature</li>
    <li>Feature</li>
    </ul>
    </div>

My question is similar to the following but I was unable to get the solution suggested by Russ Cam to work.

我的问题与下面类似,但是我无法得到Russ Cam建议的解决方案。

Wrap three repeating div groups into one using jQuery

使用jQuery将三个重复的div组打包成一个

Thanks in advance.

提前谢谢。

1 个解决方案

#1


7  

Try this:

试试这个:

$(document).ready(function(){
 $('h3').each(function(){
  $(this).add( $(this).next() ).wrapAll('<div class="box"></div>');
 })
})

#1


7  

Try this:

试试这个:

$(document).ready(function(){
 $('h3').each(function(){
  $(this).add( $(this).next() ).wrapAll('<div class="box"></div>');
 })
})