首页 > 解决方案 > 隐藏表格行的包含超链接文本,直到搜索到

问题描述

所以我已经编写了大部分程序,但我基本上需要表格不占用任何空间并保持隐藏,直到有人搜索列表中的内容。行中的所有项目都是超链接,因此必须保留为 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>

标签: javascripthtmlcss

解决方案


只需给出最初的trsa displaynone然后在每次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>


推荐阅读