[英]How do I remove all <br /> tags that aren't within <p> tags using regex?
[英]How do I remove 'p' tags with querySelector
我正在使用querySelector删除其中包含我的'p'标签; HTML中的最后三个; 但是用我编写的代码无法正常工作
// HTML
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
I have an <h1>This is my todo list</h1>
// with five <p> tags
<p>I have to study databases </P>
<p>I have to study jquery</P>
<p> I have to continue my workouts</P>
<p>I have to receive my degree </P>
<p>I have to thank my instructors</P>
<script src="note.js"></script>
</body
</html>
// JavaScript的
const paragraphs = document.querySelector('p')
paragraphs.forEach(function(paragraph){
if(paragraph.textContent.includes('my')) {
paragraph.remove()
}
})
我希望从浏览器中删除最后三个<p>
标记,但是什么也没发生; 我也在浏览器控制台上尝试过
// 先感谢您
Document.querySelector()
返回第一个匹配的元素。 由于它不返回NodeList ,因此结果没有forEach()
。 要定位所有元素,您必须使用Document.querySelectorAll()
:
Document方法
querySelectorAll()
返回一个静态(非实时) NodeList,它表示与指定选择器组匹配的文档元素的列表。
更改
const paragraphs = document.querySelector('p')
至
const paragraphs = document.querySelectorAll('p')
const paragraphs = document.querySelectorAll('p') paragraphs.forEach(function(paragraph){ if(paragraph.textContent.includes('my')) { paragraph.remove() } });
<p>I have to study databases </p> <p>I have to study jquery</p> <p> I have to continue my workouts</p> <p>I have to receive my degree </p> <p>I have to thank my instructors</p>
请注意:尽管可以,但是您不应使用大写P
来关闭标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.