如何使用JavaScript在HTML页面中查找文本?
我下面的代码试图在页面的任何位置找到文本“Hello mate”,如果它存在,则显示一个警报如何使用JavaScript在HTML页面中查找文本?,javascript,html,Javascript,Html,我下面的代码试图在页面的任何位置找到文本“Hello mate”,如果它存在,则显示一个警报 var array = Array.from(document.querySelectorAll('div')) .find(el => el.textContent.includes("Hello mate")); if (array){ alert("hi!") } 它没有给出错误,但是没有出现警报。有什么建议吗
var array = Array.from(document.querySelectorAll('div'))
.find(el => el.textContent.includes("Hello mate"));
if (array){
alert("hi!")
}
它没有给出错误,但是没有出现警报。有什么建议吗?
$(文档).ready(函数(){
$(“p”)。单击(函数(){
$(this.hide();
});
var array=array.from(document.querySelectorAll('div'))
.find(el=>el.textContent.includes(“Hello mate”);
if(数组){
警报(“嗨!”)
}
});代码>
如果你点击我,我就会消失
点击我走开
也点击我
你好,伙计
您在哪里运行此代码?如果代码在创建div之前运行(例如在
中),那么它将无法工作。它是内容脚本中Chrome扩展的一部分。在清单中,内容脚本被设置为在“document_end”运行。jQuery在这里是不相关的。