admin管理员组

文章数量:1333442

How can I separate in sections a document with headings?

Convert this

<h1>chapter 1</h1>
<p>lorem ipsum</p>
<p>sit amet</p>
<h1>chapter 2</h1>
<p>lorem ipsum</p>
<p>sit amet</p>

into this

<div class="chapter">
  <h1>chapter 1</h1>
  <p>lorem ipsum</p>
  <p>sit amet</p>
</div>
<div class="chapter">
  <h1>chapter 2</h1>
  <p>lorem ipsum</p>
  <p>sit amet</p>
</div>

I guess with jQuery this is easy, but I haven't figured how yet.

How can I separate in sections a document with headings?

Convert this

<h1>chapter 1</h1>
<p>lorem ipsum</p>
<p>sit amet</p>
<h1>chapter 2</h1>
<p>lorem ipsum</p>
<p>sit amet</p>

into this

<div class="chapter">
  <h1>chapter 1</h1>
  <p>lorem ipsum</p>
  <p>sit amet</p>
</div>
<div class="chapter">
  <h1>chapter 2</h1>
  <p>lorem ipsum</p>
  <p>sit amet</p>
</div>

I guess with jQuery this is easy, but I haven't figured how yet.

Share edited May 22, 2011 at 23:00 user1385191 asked May 22, 2011 at 22:47 VictorVictor 24k31 gold badges93 silver badges131 bronze badges 3
  • Will there always be 2 P tags after the H1 tag or is it n P tags after the H1 tag? – lomaxx Commented May 22, 2011 at 22:52
  • I guess my question is why can't you just section based on the h1 tags, rather than wrapping them in divs? – Jimmy Sawczuk Commented May 22, 2011 at 23:02
  • I did it after a fair bit of work in pure JS for those curious: jsfiddle/Wya2H/2 – user1385191 Commented May 23, 2011 at 0:33
Add a ment  | 

3 Answers 3

Reset to default 10

Try something like this:

$('h1').each( function(){
    $(this).nextUntil('h1').andSelf().wrapAll('<div class="chapter">');
});

Example: http://jsfiddle/redler/YVF2w/

I guess with jQuery this is easy, but I haven't figured how yet.

Don't do this with jQuery. Write this into your HTML, or use whatever view / template engine your using to write your HTML like this.

Also there is such a thing as <section> and <header> I would remend using them (but since they are HTML5 you might need html5 shim).

Of course if you want to do it witout jQuery, it's not that hard:

/* Convert a list to an array
 */
function listToArray(list) {
  var result = [],
      i = list.length;
  while (i--) result[i] = list[i];
  return result;
}


/* Wrap elements with tagName in a div until next sibiling
 * with same tagName
 */
function wrapSiblings(tag) {
  tag = tag.toLowerCase();

  // Take elements out of document while processing so
  // need to convert NodeList to array
  var el, els = listToArray(document.getElementsByTagName(tag));
  var div = document.createElement('div');
  div.className = 'chapter';
  var oDiv, oEl;

  for (var i=0, iLen=els.length; i<iLen; i++) {
    el = els[i];
    oEl = el.nextSibling;
    oDiv = div.cloneNode(false);

    // Shift all siblings into Div
    while (oEl && !(oEl.tagName && oEl.tagName.toLowerCase() == tag)) {
      oDiv.appendChild(oEl);

      // Moved sibling, get nextSibling - live list!
      oEl = el.nextSibling;
    }

    // Replace el with div, then put el into div
    el.parentNode.insertBefore(oDiv, el);
    oDiv.insertBefore(el, oDiv.firstChild);
  }
}

本文标签: javascriptSeparate an HTML document with headings into sectionsStack Overflow