如何删除<a> </a>标记而不影响文本?

jua*_*uan 1 javascript text document

我尝试<a> </a>使用JavaScript示例将标记删除为多个链接:

<a href="somelink.com/1"> content1
</a>
<a href="somelink.com/2"> content12
</a>
<a href="somelink.com/3"> content13
</a>
<a href="somelink.com/3"> content14
</a> ........ ect ...
Run Code Online (Sandbox Code Playgroud)

使它保持这种方式而不会影响文本:

content1
content2
content3
content4
Run Code Online (Sandbox Code Playgroud)

我尝试使用此代码来完成此操作,但未成功:

<script>
document.querySelectorAll('a[href^="somelink.com"]').forEach(
  x => a.href = "  "
)
</script>
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助

Mah*_*Ali 5

您将其更改outerHTMLinnerHTML代码如下

function removeTags(){
  document.querySelectorAll('a').forEach(a => {
    a.outerHTML = a.innerHTML
  })
  console.log(document.body.innerHTML)
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="somelink.com/1"> content1</a>
<a href="somelink.com/2"> content12</a>
<a href="somelink.com/3"> content13</a>
<a href="somelink.com/3"> content14</a>
<button onclick="removeTags()">Remove Tags</button>
Run Code Online (Sandbox Code Playgroud)