Home > Mobile >  Remove b tag within an a tag
Remove b tag within an a tag

Time:10-17

I have a html files that contains a lot of elements like that:

<p>text<p>
<p>more text<p>
<p>text <a href="#"><b>link1</b></a><p>
<p>text<p>
<ul>
  <li>A</li>
  <li>B</li>
</uli>
<p>text <a href="#"><b>linkABC</b></a><p>
<p>more text<p>
<a href="#"><b>link here</b></a>

The structure of the document may vary.

Is there anyway to remove that b tags within a tags? I want to run that html processing using nodejs.

CodePudding user response:

A solution using pure JavaScript for you

let links = document.querySelectorAll('a')
links.forEach(link => {
  let text = link.querySelector('b').innerText
  link.innerHTML = text
})

// check the updated result
let newLinks = document.querySelectorAll('a')
newLinks.forEach(link => {
  console.log(link)
})
<p>text<p>
<p>more text<p>
<p>text <a href="#"><b>link1</b></a><p>
<p>text<p>
<ul>
  <li>A</li>
  <li>B</li>
</uli>
<p>text <a href="#"><b>linkABC</b></a><p>
<p>more text<p>
<a href="#"><b>link here</b></a>

  • Related