我试图让我的搜索栏隐藏所有元素,直到用户真正搜索它们。` 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/