[英]Remove content of p tag
Is there a way to remove certain content from a p
tag with JavaScript? 有没有办法使用JavaScript从
p
标签中删除某些内容?
If I have 5 p
tags with the strings First note
all the way to Fifth note
, I would like to loop through with querySelectorAll
and then use the remove
functionality to remove the string note
from the p
tags. 如果我有5个带有字符串的
p
标签,则从First note
一直到Fifth note
,我想遍历querySelectorAll
,然后使用remove
功能从p
标签中删除字符串note
。
This is how far I have managed, but I lack the functionality to specify a string in the p
tag to delete: 这是我已经完成的工作,但是我缺少在
p
标签中指定要删除的字符串的功能:
const pTag = document.querySelectorAll('p')
pTag.forEach(function (p) {
p.remove()
})
You cannot use the remove()
method to only remove part of the string. 您不能使用
remove()
方法仅删除部分字符串。 You can use the replace()
method instead: 您可以改用
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.