简体   繁体   中英

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

I am not trying to do anything to the content (at least not yet.) What I am trying to do is search for specific tags and replace them with another one.

I got this HTML that is filled with these tags with the SAME ID (I know this is horrible!)

<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>

I am trying to write a function that would search for every <div dir="rtl" id="book-container"> and delete it without deleting the inner text. And then search for every </div> and delete it as well. Then, wrap the entire thing in a <p> tags.

The result should be something like this:

<p>
some text here #1

some text here #2

some text here #3

.
.
.

some text here #49

some text here #50
</p>

Here is my approach to the problem with a comment explaining each step.

I took a sample of three elements and enclosed them in a body tag:

 /* 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>

Here is a fiddle.js where you can see the result.

replaceWith is probably what you are after here

 //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>

You may also want to investigate textContent and how it differs from innerText

UPDATED:

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM