javascript - 使用 domparser 围绕代码块创建一个新的 div 容器

标签 javascript domparser

带有字符串:

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

我想在 header 之前插入一个 div 容器,并将下一个 header 之前的内容作为这个新 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,但位于标题之后。如何调整它以在标题之前创建标题?

最佳答案

您可以将当前元素添加到 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);

关于javascript - 使用 domparser 围绕代码块创建一个新的 div 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50985606/

相关文章:

javascript - Eclipse - 使用 html 标签格式化 javascript 时出错

javascript - 选择器非和 sibling

javascript - 使用 JavaScript 解析 XML

javascript - 如何将文档 CSS 属性应用于已解析的元素而不将其附加到 DOM

java - 在 Java 中从 DOM 解析器创建对象?

angular - DOMParser Typescript- 解析 HTML

javascript - 使用 php 动态更改选择选项

javascript - javascript : swapping technique, 添加随机文本

javascript - DOMParser().parseFromString() 未在 Firefox 中给出响应

javascript - Node js中Promise是同步还是异步