使用domparser在代码块周围创建一个新的div容器

时间:2018-06-22 10:07:30

标签: javascript domparser

带有以下字符串:

<h1>aa</h1><p>xx</p><p>yy</p><h1>aaa</h1><p>xxx</p><p>yyy</p>

我想在标题之前插入一个div容器,并将下一个标题之前的内容作为此新div中的内容

<div id="aa"><h1>aa</h1><p>xx</p><p>yy</p></div><div id="aaa"><h1>aaa</h1><p>xxx</p><p>yyy</p></div>

到目前为止,这就是我所做的

let s = "<h1>aa</h1><p>xx</p><p>yy</p><h1>aaa</h1><p>xxx</p><p>yyy</p>";

const parser = new DOMParser();
const doc = parser.parseFromString(s, 'text/html');
const elems = doc.body.querySelectorAll('*');

[...elems].forEach(el => {
  if (el.textContent !== '' && el.matches('H1')) {
    // create div container
    var div = document.createElement('div');

    //Set Id 
    div.id = el.textContent;

    // Get the next sibling next until H1
    var siblings = nextUntil(el, 'H1');
    for (var i = 0; i < siblings.length; i++) {
      // move sibling into div
      div.appendChild(siblings[i]);
    }

    // insert div before el in the DOM tree
    el.parentNode.insertBefore(div, el.nextSibling);
  }
});

function nextUntil(elem, selector) {
  // Setup siblings array
  var siblings = [];
  // Get the next sibling element
  elem = elem.nextElementSibling;
  // As long as a sibling exists
  while (elem) {
    // If we've reached our match
    if (elem.matches(selector))
      break;

    // Otherwise, push it to the siblings array
    siblings.push(elem);

    // Get the next sibling element
    elem = elem.nextElementSibling;
  }
  return siblings;
}

console.log(doc.body.innerHTML);

哪个很好地创建了div,但是在标题之后。如何调整它以在标题之前创建标题?

1 个答案:

答案 0 :(得分:1)

您可以将当前元素添加到div容器中

//Prepend current element
div.insertBefore(el, div.firstChild);

let s = "<h1>aa</h1><p>xx</p><p>yy</p><h1>aaa</h1><p>xxx</p><p>yyy</p>";

const parser = new DOMParser();
const doc = parser.parseFromString(s, 'text/html');
const elems = doc.body.querySelectorAll('*');

[...elems].forEach(el => {
    if (el.textContent !== '' && el.matches('H1')) {
    // create div container
    var div = document.createElement('div');

    //Set Id 
    div.id = el.textContent;

    // Get the next sibling next until H1
    var siblings = nextUntil(el, 'H1');
    for (var i = 0; i < siblings.length; i++) {
        // move sibling into div
        div.appendChild(siblings[i]);
    }

    // insert div before el in the DOM tree
    el.parentNode.insertBefore(div, el.nextSibling);
    
    //Prepend current element
    div.insertBefore(el, div.firstChild);
  }
});

function nextUntil(elem, selector) {
    // Setup siblings array
    var siblings = [];
    // Get the next sibling element
    elem = elem.nextElementSibling;
    // As long as a sibling exists
    while (elem) {
        // If we've reached our match
        if (elem.matches(selector))
        break;

        // Otherwise, push it to the siblings array
        siblings.push(elem);

        // Get the next sibling element
        elem = elem.nextElementSibling;
    }
    return siblings;
}

console.log(doc.body.innerHTML);


您可以附加当前元素,然后附加选定的兄弟姐妹

// Get the next sibling next until H1
var siblings = nextUntil(el, 'H1');

// insert div before el in the DOM tree
el.parentNode.insertBefore(div, el.nextSibling);

//Append the current element
div.appendChild(el);
// move sibling into div
for (var i = 0; i < siblings.length; i++) {
  div.appendChild(siblings[i]);
}

let s = "<h1>aa</h1><p>xx</p><p>yy</p><h1>aaa</h1><p>xxx</p><p>yyy</p>";

const parser = new DOMParser();
const doc = parser.parseFromString(s, 'text/html');
const elems = doc.body.querySelectorAll('*');

[...elems].forEach(el => {
  if (el.textContent !== '' && el.matches('H1')) {
    // create div container
    var div = document.createElement('div');

    //Set Id 
    div.id = el.textContent;

    // Get the next sibling next until H1
    var siblings = nextUntil(el, 'H1');

    // insert div before el in the DOM tree
    el.parentNode.insertBefore(div, el.nextSibling);
    div.appendChild(el);
    for (var i = 0; i < siblings.length; i++) {
      // move sibling into div
      div.appendChild(siblings[i]);
    }


  }
});

function nextUntil(elem, selector) {
  // Setup siblings array
  var siblings = [];
  // Get the next sibling element
  elem = elem.nextElementSibling;
  // As long as a sibling exists
  while (elem) {
    // If we've reached our match
    if (elem.matches(selector))
      break;

    // Otherwise, push it to the siblings array
    siblings.push(elem);

    // Get the next sibling element
    elem = elem.nextElementSibling;
  }
  return siblings;
}

console.log(doc.body.innerHTML);