简体   繁体   English

jQuery在div中包装元素集

[英]jQuery wrap sets of elements in div

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. 提前致谢。

Try this: 试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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