How to wrap h1 and follow content in div? [duplicate]

元气小坏坏 提交于 2019-12-10 21:43:13

问题


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>

回答1:


Try using wrapAll and group the h1 and all p tags

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

DEMO: http://jsfiddle.net/zPafK/

or http://jsfiddle.net/zPafK/2/ (added some styles)



来源:https://stackoverflow.com/questions/16899698/how-to-wrap-h1-and-follow-content-in-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!