javascript - 隐藏表格行的包含超链接文本,直到搜索到
问题描述
所以我已经编写了大部分程序,但我基本上需要表格不占用任何空间并保持隐藏,直到有人搜索列表中的内容。行中的所有项目都是超链接,因此必须保留为 HTML 的一部分。希望搜索栏仍然像现在一样实时更新;因此,当您仅搜索“sup”时,包含“supreme”的行应该已经填充并显示出来。
例如:所有行都应该保持隐藏,当您搜索“至尊”时,只有包含至尊的行应该出现。
function myFunction() {
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
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 = "";
display = "block";
} else {
tr[i].style.display = "none";
}
}
}
}
* {
box-sizing: border-box;
}
#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
#myTable {
border-collapse: collapse;
width: 100%;
border: 1px solid #ddd;
font-size: 18px;
}
#myTable th, #myTable td {
text-align: left;
padding: 12px;
}
#myTable tr {
border-bottom: 1px solid #ddd;
}
#myTable tr.header, #myTable tr:hover {
background-color: #f1f1f1;
}
<h2>Search Contracts Library</h2>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for contracts.." title="Type in a name">
<div id="myDIV">
<table id="myTable">
<td><a href="https://www.w3schools.com/html/" target="_blank">nike</a> </td>
<td>Germany</td>
</tr>
<tr>
<td><a href="https://www.w3schools.com/html/" target="_blank">yeezy</a> </td>
<td>Sweden</td>
</tr>
<tr>
<td><a href="https://www.w3schools.com/html/" target="_blank">adidas</a> </td>
<td>UK</td>
</tr>
<tr>
<td><a href="https://www.w3schools.com/html/" target="_blank">supreme</a> </td>
<td>Italy</td>
</tr>
<tr>
<td><a href="https://www.w3schools.com/html/" target="_blank">palace</a> </td>
<td>UK</td>
</tr>
<tr>
<td><a href="https://www.w3schools.com/html/" target="_blank">vans</a> </td>
<td>France</td>
</tr>
</table>
</div>
解决方案
只需给出最初的tr
sa display
,none
然后在每次tr
输入更改时迭代:
function myFunction() {
const filterBy = document.querySelector('#myInput').value.toLowerCase().trim();
document.querySelectorAll('#myTable tr')
.forEach((tr) => {
if (filterBy && tr.children[0].textContent.toLowerCase().includes(filterBy)) {
tr.style.display = 'block';
} else tr.style.display = 'none';
});
}
* {
box-sizing: border-box;
}
#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
#myTable {
border-collapse: collapse;
width: 100%;
border: 1px solid #ddd;
font-size: 18px;
}
#myTable th, #myTable td {
text-align: left;
padding: 12px;
}
#myTable tr {
border-bottom: 1px solid #ddd;
display: none;
}
#myTable tr.header, #myTable tr:hover {
background-color: #f1f1f1;
}
<h2>Search Contracts Library</h2>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for contracts.." title="Type in a name">
<div id="myDIV">
<table id="myTable">
<td><a href="https://www.w3schools.com/html/" target="_blank">nike</a> </td>
<td>Germany</td>
</tr>
<tr>
<td><a href="https://www.w3schools.com/html/" target="_blank">yeezy</a> </td>
<td>Sweden</td>
</tr>
<tr>
<td><a href="https://www.w3schools.com/html/" target="_blank">adidas</a> </td>
<td>UK</td>
</tr>
<tr>
<td><a href="https://www.w3schools.com/html/" target="_blank">supreme</a> </td>
<td>Italy</td>
</tr>
<tr>
<td><a href="https://www.w3schools.com/html/" target="_blank">palace</a> </td>
<td>UK</td>
</tr>
<tr>
<td><a href="https://www.w3schools.com/html/" target="_blank">vans</a> </td>
<td>France</td>
</tr>
</table>
</div>
推荐阅读
- laravel-5 - 因为它显示未定义的偏移量:0,播种机 laravel?
- fastify - fastify 中同一处理程序的多个路径
- python - 在vim中对列表中的项目进行排序
- linux - 在 Linux 主机上运行 .NET Core 3.1 和 SQL Server 2017 Linux 映像时出现问题
- php - PHP 和 htaccess 中的虚 URL 不断重定向
- mongodb - 处理完整的 mongodb-atlas 数据库
- elasticsearch - 无法使用公共 ip 连接到 Elasticsearch EC2 端口 9200
- javascript - 我想找到与数组 Java Script 中的字符串完全匹配
- git - 我应该采取什么样的行动来获取“master”分支中的最新源代码?
- python - 无法调用“wm”命令:应用程序已被销毁