求一段JS代码,要求搜索当前页面,如果发现当前页面中有设定的字符,就给出提示

比如,当前页面中含有“123”那么就不管他了,如果当前页面有“456”那就让他跳转链接

还有就是检测输入框中的内容,如果含有“567”input提交时把“567”给他换成789

<html>
<script>
  function searchWords() {
    var words = document.getElementById('search').value;
    if (words != '') {
      var innerHTML = document.body.innerHTML;
      if (innerHTML.indexOf(words) != -1) {
        alert('找到了');
        if (words == '123') {
          alert('123不作操作');
        } else if (words == '456') {
          alert('456跳转页面');
          window.location.href = 'http://www.baidu.com';
        } else if (words == '567') {
          alert('567替换内容');
          document.body.innerHTML = innerHTML.replace(new RegExp(/(567)/g),'789');
        }
      }
    }
  }
</script>
<body>
<div>123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ</div>
<div><input id='search' type='text' placeholder='请输入查询字符' /><button onclick='searchWords();'>查询</button></div>
</body>
</html>

温馨提示:答案为网友推荐,仅供参考
相似回答