javascript - 我怎样才能删除一个
问题描述
也许这是一个基本问题,但我应该强调我对这些事情知之甚少。本质上,在我的页面中,我有类似的内容:
<h2>Some text there</h2>
<h2>Other text there</h2>
我想把它简单地变成:
Some text there
Other text here
我的意思是,本质上,我想删除文本周围的 H2。删除它会非常容易,但不幸的是我只能间接访问代码。有没有办法使用Javascript动态删除它?
解决方案
您可以遍历所有h2
元素并将每个元素替换为元素的文本节点textContent
(带有document.createTextNode
):
document.querySelectorAll('h2').forEach(e => e.parentNode.replaceChild(document.createTextNode(e.textContent), e))
<h2>Some text there</h2>
<br/>
<h2>Other text there</h2>
推荐阅读
- python - Python 3 中的计算器循环
- php - 电子邮件管道有效,但退回电子邮件已发送
- sql-server - 如何在带有 CONCAT 的 SELECT 语句中使用 CASE
- python - 将 pandas 数据帧导出为 JSON 时如何解决 OverflowError
- angular - Angular 2+ 中属性的规范化规则是什么?
- javascript - 如何用 yazl 压缩缓冲区?
- azure - Azure 扩展事件目标文件
- flutter - 如何在文本字段中添加图标
- c# - 如何使用 for 循环将编号对象属性设置为数组中的值?
- azure - 当我们使用 Azure 数据湖存储作为 Azure 分析服务的数据源时,是否支持 Parquet 文件格式?