javascript - 如何从 html 内容中删除所有链接
问题描述
假设我有一个 html 内容,例如,
<p id="p_content">
Test
<a href='https://google.com'>google</a>
Test
<a href='https://example.co'>example</a>
Hello
<a href='https://Test.com'>test</a>
</p>
如何从中删除所有链接。我试过了,
var content = document.querySelector('#p_content');
var a = content.querySelectorAll('a');
var texts = [].slice.call(a).map(function(val){
return val.innerHTML;
});
console.log(texts);
document.querySelector('#p_content').innerHTML = texts;
它不返回任何值,而是一个空对象。
解决方案
您可以使用remove()
document.querySelectorAll('#p_content a').forEach(el => el.remove())
<p id="p_content">
Test
<a href='https://google.com'>google</a>
Test
<a href='https://example.co'>example</a>
Hello
<a href='https://Test.com'>test</a>
</p>
推荐阅读
- html - 在图形标签内使用标签是否正确?
- amazon-web-services - 在 S3 上创建文件文件夹
- android - 对齐卡片内的搜索图标和文本
- mysql - MySQL 服务器在没有更改后消失了
- nlog - 在自定义目标中重用 NLogs FileTargets FilePathLayout 功能
- vb6 - 来自 Interop 对象的 VB6 内存泄漏
- c# - 使用 MS-Graph API 搜索特定邮件时出现问题
- vue.js - 使用 Vue apollo 无限加载失败的查询
- javascript - 通过 chrome 扩展程序登录 laravel nova 网络应用程序。419 错误 csrf 令牌不匹配
- scala - 新 StreamingContext 上的 NoSuchMethodError