您好我想使用jQuery来包装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>
期望的结果:
<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>
我的问题类似于以下内容,但我无法得到Russ Cam建议的解决方案。
Wrap three repeating div groups into one using jQuery
提前致谢。
答案 0 :(得分:9)
试试这个:
$(document).ready(function(){
$('h3').each(function(){
$(this).add( $(this).next() ).wrapAll('<div class="box"></div>');
})
})