p 用JavaScript标记?
p
如果我有5个 P 带字符串的标记 First note 一直到 Fifth note ,我想用 querySelectorAll 然后使用 remove note 从 P 标签。
P
First note
Fifth note
querySelectorAll
remove
note
这是我管理的程度,但我缺少在列表中指定字符串的功能 P 要删除的标记:
const pTag = document.querySelectorAll('p') pTag.forEach(function (p) { p.remove() })
remove() 方法仅移除字符串的一部分。你可以使用 replace() 方法:
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>