javascript - 如何使用 JavaScript 在 HTML 页面中查找文本?
问题描述
我下面的代码试图在页面上的任何位置找到文本“Hello mate”,如果存在则显示警报。
var array = Array.from(document.querySelectorAll('div'))
.find(el => el.textContent.includes("Hello mate"));
if (array){
alert("hi!")
}
它没有给出错误,但是没有出现警报。有什么建议么?
解决方案
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
var array = Array.from(document.querySelectorAll('div'))
.find(el => el.textContent.includes("Hello mate"));
if (array){
alert("hi!")
}
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="source_your_file"></script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
<div>Hello mate</div>
</body>
</html>
也许你错过了在你的标题上添加你的文件 jquery 或者你的 jquery 首先需要调用 $(document).ready(function(){} 。我有使用 jquery 的例子,也许它可以帮助你
推荐阅读
- python - AWS Lambda 中的 FFMPEG 只会输出 5 秒的转换视频
- python - 为什么即使我已指定我的链表遍历终止,我也会收到超时错误?
- amazon-web-services - 将 sql 转储文件从 EC2 复制到 RDS 数据库时出现“1046 No Database Selected”错误
- amazon-s3 - 在 EC2 中使用 AWS S3 文件网关,为什么它只能在公有子网中工作,而不能在私有子网中工作?
- maven - PMD - maven、sonarqube 和 java 版本之间的混淆
- javascript - 如果连接脚本,则不会触发 Angular DOMContentLoaded
- python - 加速 AES_CBC 模式(使用 AES_ECB 模式)
- python - 从 Colab 上的 summarizer 导入 Summarizer 出错
- c# - 如何使用阻塞的 stream.Read() 停止线程?关于多线程
- qt - 如何通过单击 Qt Widget Application 中的按钮转到其他表单