Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript在HTML页面中查找文本?_Javascript_Html - Fatal编程技术网

如何使用JavaScript在HTML页面中查找文本?

如何使用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!") } 它没有给出错误,但是没有出现警报。有什么建议吗

我下面的代码试图在页面的任何位置找到文本“Hello mate”,如果它存在,则显示一个警报

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在这里是不相关的。