如何包装H1并关注div中的内容? [英] How to wrap h1 and follow content in div?

查看:95
本文介绍了如何包装H1并关注div中的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何包装h1并遵循div中的内容.这是原始结构:

I need to know how to wrap h1 and follow content in div. This is the original structure:

<h1>Title #1</h1>
<p>Text</p>
<p>Text</p>

<h1>Title #2</h1>
<p>Text</p>
<p>Text</p>
<p>Text</p>

<h1>Title #3</h1>
<p>Text</p>

这是我想要得到的结果:

This is the result I want to get :

<div>
  <h1>Title #1</h1>
  <p>Text</p>
  <p>Text</p>
</div>

<div>
  <h1>Title #2</h1>
  <p>Text</p>
  <p>Text</p>
  <p>Text</p>
</div>

<div>
  <h1>Title #3</h1>
  <p>Text</p>
</div>

推荐答案

尝试使用wrapAll并将h1和所有p标记分组

Try using wrapAll and group the h1 and all p tags

$(function () {
    $('h1').each(function () {
        $(this).nextUntil('h1').add(this).wrapAll('<div />');
    });
});

演示: http://jsfiddle.net/zPafK/

http://jsfiddle.net/zPafK/2/(添加了一些样式)

这篇关于如何包装H1并关注div中的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆