javascript - 删除 p 标签的内容
问题描述
有没有办法p
使用 JavaScript 从标签中删除某些内容?
如果我有 5 个p
带有字符串First note
的标签Fifth note
,我想循环使用querySelectorAll
,然后使用该功能从标签remove
中删除字符串。note
p
这是我管理的程度,但我缺乏在p
标签中指定要删除的字符串的功能:
const pTag = document.querySelectorAll('p')
pTag.forEach(function (p) {
p.remove()
})
解决方案
您不能使用该remove()
方法仅删除部分字符串。您可以改用该replace()
方法:
const pTag = document.querySelectorAll('p');
pTag.forEach(function(p) {
p.innerHTML = p.innerHTML.replace('note', '');
});
<p>This is the first note</p>
<p>This is the second note</p>
<p>This is the third note</p>
<p>This is the fourth note</p>
<p>This is the fifth note</p>
推荐阅读
- mongodb - 未按照架构插入数据
- python - AWS Cloud9:Python 虚拟环境实际上并未激活。使用系统范围的 python
- agora.io - 我在哪里可以获得帮助教程视频来优化应用程序?
- reactjs - React / Enzyme:运行 Jest / Enzyme 测试时出现 Invariant Violation 错误
- sql - Add another column in the below query
- javascript - 以编程方式将路由注入 JSX 路由器 switch 语句
- excel - How to get the earliest time each day and the last?
- sql - 关于 SQL 查询的建议
- android - How to set a wallpaper as the homescreen full screen
- sql - 在 PostgreSQL 中合并两个视图