开发者

jQuery wrap sets of elements in div

开发者 https://www.devze.com 2022-12-16 03:47 出处:网络
Hello I would like to use jQuery to wrap sets of elements in a div HTML: <h3>Title</h3> <ul>

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>
    <开发者_如何学Cli>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>');
 })
})
0

精彩评论

暂无评论...
验证码 换一张
取 消