javascript - 在搜索之前隐藏所有元素的搜索栏

标签 javascript jquery html css

我试图让我的搜索栏隐藏所有元素,直到用户真正搜索它们。` Heres a JSfiddel

    <div id="search">
              <form>
  <input type="text" name="search" id="myInput" onkeyup="mFunction()" 
   placeholder="Search ?..">
 </form>
 <nav>
 <table id="myTable">
 <tr class="header">
<th style="width:500px;">Question</th>
</tr>
<tr>  
 <td onclick="window.location.href='#web'">What is the company email?</td>
</tr>
 <tr>
<td onclick="window.location.href='#web'">Is the website currently under 
development?</td>
 </tr>
 <tr>
<td onclick="window.location.href='#game'">Why are the games not working 
online?</td>
  </tr>
 <tr>
  <td onclick="window.location.href='#game'">What is the next game or games 
 that you are working on?</td>

</tr>

<tr>
 <td onclick="window.location.href='#game'">Are you working on Modern Jewels 
 anymore?</td>
</tr>
  <tr>
<td onclick="window.location.href='#game'">What are the controls for Modern 
 Jewels?</td>
    </tr>
 </table>
 </div>
 </nav>`

JavaScript/jQuery

    function mFunction() {
  // Declare variables 
  var input, filter, table, tr, td, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");

  // Loop through all table rows, and hide those who don't match the search 
  query
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    } 
  }
}

你知道吗,它是一个常见问题解答页面,我希望用户能够在搜索过程中搜索问题而不需要大量的问题。

感谢任何帮助,感谢 :D

最佳答案

由于表格中包含所有搜索结果,因此您可以使用 jquerys .hide() 和 .show() 动画来切换整个表格。

隐藏表格,直到用户搜索使用 .hide() 隐藏表格

$("#myTable").hide();

关于javascript - 在搜索之前隐藏所有元素的搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44815327/

相关文章:

javascript - popoto.js - 预定义密码查询

javascript - FileReader 返回未定义

jQuery 如何使用 JQuery 检索输入文本字段的名称

javascript - 当浏览器检索缓存的 html 页面时,JavaScript 会再次运行吗?

javascript - Angular : When to use "[]" in modules and controllers

jquery - Bootstrap - 将远程 Google Chart 加载到 Modal 中

jquery - 如何用jquery实现递归模板

html - 如何风格化 3 个 div 以相对适合 100% 窗口大小

带有 Fancybox 的 HTML5 视频

javascript - 禁用和重新启用提交按钮并考虑到跨浏览器兼容性