javascript-删除包含特定文本的段落
我有如下段落,我想删除包含文本“do”的段落 什么也不做 我不知道 试试看 如果可以的话,抓住我 我希望结果是 试试看 如果可以的话,抓住我javascript-删除包含特定文本的段落,javascript,html,dom,Javascript,Html,Dom,我有如下段落,我想删除包含文本“do”的段落 什么也不做 我不知道 试试看 如果可以的话,抓住我 我希望结果是 试试看 如果可以的话,抓住我 普通的javascript,而不是jQuery。谢谢。你是想说你在帖子中指定的每一行都有标签吗 然后你可以跟着 $("p").each(function(){ if($(this).text().indexOf("do") != -1) $(this).attr("hidden", true); // Or you can remove,
普通的javascript,而不是jQuery。谢谢。你是想说你在帖子中指定的每一行都有标签吗 然后你可以跟着
$("p").each(function(){
if($(this).text().indexOf("do") != -1)
$(this).attr("hidden", true);
// Or you can remove, by accessing parent element and remove this from the
parent
})
})
如果我理解正确,你可能会想要这样的东西:
els = document.getElementsByTagName('p');
for(let i = 0; i < els.length; i++){
if(els[i].innerHTML.includes('do')){
els[i].parentElement.removeChild(els[i]);
}
}
els=document.getElementsByTagName('p');
for(设i=0;ivar p=document.querySelectorAll('p');
for(设i=0;i
上面的代码应该可以实现您想要的功能。
函数upd(){
const ps=document.queryselectoral('p')
ps.forEach(功能(p){
if(p.textContent.toLowerCase().includes('do')){
p、 删除()
}
})
}
什么也不做
我不知道
试试看
如果可以的话,抓住我
我希望结果是
试试看
如果可以的话,抓住我
更新这将删除所有包含“the”的段落。这只是一个非常基本的例子
HTML
到目前为止,您尝试了什么?请记住,so不是一个代码请求站点。其主要思想是理解问题并用您拥有的代码解决问题。
var p = document.querySelectorAll('p');
for(let i = 0; i < p.length; i++)
{
// remove toLowerCase if you want case sensitive.
if (p[i].textContent.toLowerCase().indexOf('do') != -1)
{
p[i].remove(i);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<link rel="stylesheet" href="style.css">
<title>todo-app</title>
</head>
<body>
<div class="div1">
<h1>This is our todo-app</h1>
<h2>Hello there!</h2>
</div>
<p>Paragraph 1</p>
<p>Paragraph the 2</p>
<p>Paragraph 3</p>
<p>Paragraph the 4</p>
<p>Paragraph 5</p>
<script src="todo-app.js"></script>
</body>
</html>
const ps = document.querySelectorAll('p');
ps.forEach(p =>{
if (p.textContent.includes('the')){
p.remove();
}
})