在搜索之前隐藏包含超链接文本的表行

时间:2018-04-25 00:31:49

标签: javascript html css

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

1 个答案:

答案 0 :(得分:0)

最初只需tr 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>
import pandas as pd

# Create DataFrame
df = pd.DataFrame(
{'id':[2967, 5335, 13950, 6141, 6169],\
 'Player': ['Cedric Hunter', 'Maurice Baker' ,\
            'Ratko Varda' ,'Ryan Bowen' ,'Adrian Caldwell'],\
 'Year': [1991 ,2004 ,2001 ,2009 ,1997],\
 'Age': [27 ,25 ,22 ,34 ,31],\
 'Tm':['CHH' ,'VAN' ,'TOT' ,'OKC' ,'DAL'],\
 'G':[6 ,7 ,60 ,52 ,81]})


df.set_index('Player', inplace=True)