javascript - 在页面 HTML 上搜索字符串后出现警报通知 "not found"

标签 javascript html

我有这个搜索代码 html

<form method="" action="">
  <div class="input-group">
   <input type="text" id="search" class="form-control" placeholder="Masukan No Hp yg Akan Dicari.." >
                  <span class="input-group-btn">
                <input type="button" id="submit_form" onclick="checkInput()" value="Cari" class="btn btn-primary">
    </div>
</form>

这是我的 JS 代码

<script>
    function checkInput() {
        var query = document.getElementById('search').value;
        window.find(query);
        return true; 

        }
</script> 

我想添加警报通知... 如果搜索在我的页面中找不到的字符串

最佳答案

不要使用window.find。如mdn说:

This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

只需搜索文档的 textContent 字符串即可:

document.querySelector('#submit_form')
  .addEventListener('click', (e) => {
    e.preventDefault();
    const query = document.querySelector('#search').value;
    console.log('Found: ' + document.body.textContent.includes(query));
  });
<form method="" action="">
  <div class="input-group">
    <input type="text" id="search" class="form-control" placeholder="Masukan No Hp yg Akan Dicari..">
    <span class="input-group-btn"></span>
                <input type="button" id="submit_form" value="Cari" class="btn btn-primary">
    </div>
</form>
<div>text1</div>
<div>text2</div>

尝试搜索 text1text3

关于javascript - 在页面 HTML 上搜索字符串后出现警报通知 "not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49937047/

相关文章:

javascript - 使用JS/jQuery将YouTube视频网址的整个播放列表放入数组中

javascript - Angular 拖放 : Splice not a function on three level

javascript - 分区隐藏在 jquery 中不起作用

javascript - 在 CSS3 中创建这个形状作为下拉 div

javascript - 带有 CSS 的 Material 设计应用栏

asp.net - 输入复选框列表

javascript - 可以禁用表中的所有字段并设置条件以启用所需的某些字段??

javascript - 使用 JsonPipe 纠正 Angular 中的缩进 json

php - mysql运行按列分组的付款总额,共享代码

javascript - HTML中的属性和属性有什么区别?