首页 > 解决方案 > 加载和警报时的 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>

标签: javascriptjqueryhtml

解决方案


||根据您的情况将运算符更改为&&运算符。

if ((document.documentElement.textContent && document.documentElement.innerText).indexOf("John") > -1) {
         alert("Matched!");
}

推荐阅读