javascript - 加载和警报时的 HTML 搜索文本
问题描述
我正在尝试在加载的页面上查找文本,如果文本匹配,那么我应该收到警报
<script type='text/javascript'>
window.onload = function() {
if ((document.documentElement.textContent || document.documentElement.innerText).indexOf("John") > -1) {
alert("Matched");
}
};
</script>
<body>
<p> Hello! John is here </p>
</body>
解决方案
||
根据您的情况将运算符更改为&&
运算符。
if ((document.documentElement.textContent && document.documentElement.innerText).indexOf("John") > -1) {
alert("Matched!");
}
推荐阅读
- angular - 如何解析对象'不包含这样的成员 Angular 7 和 VS Code 错误
- c++ - 混淆如何在 std::sort 和 std::priority_queue 中使用自定义比较函数
- grafana - 在同一个 grafana 面板中显示 20 条曲线 1 条曲线 x 用户
- excel - Excel VBA 缺少 Microsoft Outlook 15.0 参考和“加载 DLL 时出错”
- c++ - 谁能检查这个 Palindrome c++ 代码是否正确?
- flutter - 可滚动小部件内的 Flutter PageView
- node.js - Azure 服务总线长时间运行队列
- ios - 如何在 VIMVideoPlayer URL 受限中播放 vimeo 视频
- hibernate - 保存子实体时出现@PrePersist 错误
- regex - 是否有任何正则表达式可以同时提取两个字符串