Use Javascript Bookmarklet to Chunk Page Content by Headings

前端 未结 2 637
长发绾君心
长发绾君心 2021-01-24 16:12

I\'ve got an HTML page that is compiled dynamically from a database that I need to restyle and restructure. It looks messy, I know, but what I\'m working with is this (note: no

2条回答
  •  春和景丽
    2021-01-24 16:50

    You don't need jquery. Just walk the childNode pseudo-array (presumably of the body...a body element should be created even if not specified in the html), and build an output array of elements. When you hit a h1/h2/h3, you'll create a div, add it to the end of the array, as well as saving it as the "current element" which other elements will be added to. Once done, you can add those elements to the body (or put them somewhere else).

    var parent = document.body, i;
    // copy into temp array, since otherwise you'll be walking
    // an array (childnodes) whose size is changing as elements 
    // get removed from it
    var tmpArray = [];
    for (i=0; i

提交回复
热议问题