需要用jquery将h3和div包装在包装器div中
我有以下 HTML:
<div class="accordion">
<h3>My title</h3>
<div>My content</div>
<h3>My title</h3>
<div>My content</div>
<h3>My title</h3>
<div>My content</div>
</div>
我需要通过 jQuery 做的是将每个 h3 和 div 包装在另一个 div class="myDiv" 中,因此最终输出将如下所示:
<div class="accordion">
<div class="myDiv">
<h3>My title</h3>
<div>My content</div>
</div>
<div class="myDiv">
<h3>My title</h3>
<div>My content</div>
</div>
<div class="myDiv">
<h3>My title</h3>
<div>My content</div>
</div>
</div>
我将如何使用 jquery 做到这一点?
I have the following HTML:
<div class="accordion">
<h3>My title</h3>
<div>My content</div>
<h3>My title</h3>
<div>My content</div>
<h3>My title</h3>
<div>My content</div>
</div>
What I need to do via jQuery is wrap each of the h3's and div's in another div class="myDiv", so the final output would look like:
<div class="accordion">
<div class="myDiv">
<h3>My title</h3>
<div>My content</div>
</div>
<div class="myDiv">
<h3>My title</h3>
<div>My content</div>
</div>
<div class="myDiv">
<h3>My title</h3>
<div>My content</div>
</div>
</div>
How would I do this with jquery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
http://jsfiddle.net/BSQpf/
Try this:
http://jsfiddle.net/BSQpf/
像这样的东西应该可以做到:
jsFiddle 示例
请参阅 API:
nextUntil
andSelf
wrapAll
Something like this should do it:
jsFiddle example
See the API:
nextUntil
andSelf
wrapAll