如何使用 Vanilla JavaScript 搜索和替换 HTML 文件中的所有标签?

How to search and replace all tags in HTML file using Vanilla JavaScript?

我不想对内容做任何事情(至少现在还没有。)我想做的是搜索特定标签并将其替换为另一个标签。

我得到了这个 HTML,其中充满了具有相同 ID 的这些标签(我知道这很糟糕!)

<div dir="rtl" id="book-container">
some text here #1
</div>

<div dir="rtl" id="book-container">
some text here #2
</div>

<div dir="rtl" id="book-container">
some text here #3
</div>

.
.
.

<div dir="rtl" id="book-container">
some text here #49
</div>

<div dir="rtl" id="book-container">
some text here #50
</div>

我正在尝试编写一个函数来搜索每个 <div dir="rtl" id="book-container"> 并将其删除 而不会 删除内部文本。然后搜索每个 </div> 并将其删除。然后,将整个内容包装在 <p> 标签中。

结果应该是这样的:

<p>
some text here #1

some text here #2

some text here #3

.
.
.

some text here #49

some text here #50
</p>

更新:

function removeElements() {
  let parent = document.getElementById('book-container').parentElement;
  let elems = Array.from(parent.children);
  let texts = []
  let group = []
  
  let replace = () => {
    if (group.length) {
      let groupElem = document.createElement('p');
      groupElem.innerHTML = group.map(e => e.innerText).join('<br/>')
      group[0].replaceWith(groupElem);
      for (let i = 1; i < group.length; ++i) {
        group[i].remove();
      }
    }
    group = []
  };
  
  for (let elem of elems) {
    if (elem.tagName == 'DIV' && elem.id == 'book-container') { group.push(elem); }
    else { replace(); }
  }
  
  replace();
}

removeElements()
<div dir="rtl" id="book-container">
some text here #1
</div>

<div dir="rtl" id="book-container">
some text here #2
</div>

<div dir="rtl" id="book-container">
some text here #3
</div>

<p>Some Other</p>
<p>Content</p>

<div dir="rtl" id="book-container">
some text here #49
</div>

<div dir="rtl" id="book-container">
some text here #50
</div>

replaceWith 可能就是你想要的

//Get the offending nodes
let candidates = document.querySelectorAll("#book-container");
//And iterate them
for(let i = 0; i < candidates.length; i++){
  //Get a node
  let candidate = candidates[i];
  //Create a replacement, could use createTextNode if you don't want to wrap it
  //Or change the element, add styles etc if you want.
  let replacement = document.createElement("p");
  //Set the inner text from the node
  replacement.innerText = candidate.innerText;
  //Replace the node
  candidate.replaceWith(replacement);  
}
<div dir="rtl" id="book-container">
some text here #1
</div>

<div dir="rtl" id="book-container">
some text here #2
</div>

<div dir="rtl" id="book-container">
some text here #3
</div>

<p>Some Other</p>
<p>Content</p>

<div dir="rtl" id="book-container">
some text here #49
</div>

<div dir="rtl" id="book-container">
some text here #50
</div>

您可能还想调查 textContenthow it differs from innerText

这是我解决问题的方法,并附有解释每个步骤的注释。

我抽取了三个元素的样本并将它们包含在 body 标签中:

/* Create a paragraph element where the content will be displayed */
const theParagraph = document.createElement("p");
/* Choosing the elements */
const soWrongNodes = document.querySelectorAll("#book-container")
/* For each element, put its content in the paragraph then remove the 
element */
soWrongNodes.forEach(soWrongNode => {
  theParagraph.innerHTML += soWrongNode.innerHTML;
  theParagraph.innerHTML += "<br>";
  soWrongNode.remove();
})
/* Append the paragraph element to the body */
document.body.appendChild(theParagraph);
<body>
  <div dir="rtl" id="book-container">
    some text here #1
  </div>
  <div dir="rtl" id="book-container">
    some text here #2
  </div>
  <div dir="rtl" id="book-container">
    some text here #3
  </div>
</body>

这是一个fiddle.js,您可以在其中查看结果。