隐藏包含超链接文本的表格行,直到搜索



所以我已经写了大部分程序,但是我从本质上需要表才能不占用任何空间并保持隐藏状态,直到有人搜索列表中的东西为止。行中的所有项目都是超链接,因此必须保留HTML的一部分。希望搜索栏仍然像现在一样实时更新;因此,当您仅搜索" SUP"时,包含" Supreme"的行应该已经填充并出现。

例如:所有行应保持隐藏状态,当您搜索" 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>

最初只给出nonetr S A display,然后每次更改输入时通过每个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>

最新更新