简体   繁体   中英

jQuery wrap sets of elements in div

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

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.

Wrap three repeating div groups into one using jQuery

Thanks in advance.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM