如何使用 JavaScript 搜索包含所有列的表格

我正在使用表格,我想在所有列中按匹配执行搜索。以下代码仅匹配表的第一列数据。但我希望它应该按所有列数据进行搜索。如果您有任何想法我可以做到这一点,请与我分享。提前致谢!

JsFiddlehttps ://jsfiddle.net/hbrjy04m/

<!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<style>

* {

  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;

}

</style>

</head>

<body>


<h2>My Customers</h2>


<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">


    <table id="myTable">

      <tr class="header">

        <th style="width:60%;">Name</th>

        <th style="width:40%;">Country</th>

      </tr>

      <tr>

        <td>Alfreds Futterkiste</td>

        <td>Germany</td>

      </tr>

      <tr>

        <td>Berglunds snabbkop</td>

        <td>Sweden</td>

      </tr>

      <tr>

        <td>Island Trading</td>

        <td>UK</td>

      </tr>

      <tr>

        <td>Koniglich Essen</td>

        <td>Germany</td>

      </tr>

      <tr>

        <td><a href="https://teluguhitflopmovieslist.blogspot.com/">Laughing Bacchus Winecellars</a></td>

        <td>Canada</td>

      </tr>

      <tr>

        <td>Magazzini Alimentari Riuniti</td>

        <td>Italy</td>

      </tr>

      <tr>

        <td>North/South</td>

        <td><a href="https://lyricxona.blogspot.com/">UK</a></td>

      </tr>

      <tr>

        <td>Paris specialites</td>

        <td><a href="https://castxona.blogspot.com/">France</a></td>

      </tr>

    </table>


至尊宝的传说
浏览 147回答 4
4回答

紫衣仙女

您可以使用for循环遍历atd中的所有 str并搜索组合文本。function myFunction() {&nbsp; var input, filter, table, tr, td, i, txtValue;&nbsp; input = document.getElementById("myInput");&nbsp; filter = input.value.toUpperCase();&nbsp; table = document.getElementById("myTable");&nbsp; tr = table.getElementsByTagName("tr");&nbsp; for (i = 0; i < tr.length; i++) {&nbsp; &nbsp; const tableData = tr[i].getElementsByTagName("td");&nbsp; &nbsp; let allTextContent = '';&nbsp; &nbsp; for (let ind = 0; ind < tableData.length; ind++) {&nbsp; &nbsp; &nbsp; &nbsp; allTextContent += tableData[ind].innerText;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if (allTextContent) {&nbsp; &nbsp; &nbsp; if (allTextContent.toUpperCase().indexOf(filter) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; tr[i].style.display = "";&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; tr[i].style.display = "none";&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }}尽管您可以避免额外的循环并textContent改为搜索整行。一些东西也可以重构。更新了小提琴。const searchInput = document.querySelector('#myInput');const tableRows = document.querySelector('#myTable').querySelectorAll('tr');searchInput.addEventListener('input', (e) => {&nbsp; const searchInputValue = e.target.value.toLowerCase();&nbsp; tableRows.forEach(row => {&nbsp; &nbsp; const doesRowMatch = row.textContent.toLowerCase().includes(searchInputValue);&nbsp; &nbsp; if (doesRowMatch) {&nbsp; &nbsp; &nbsp; row.style.display = 'table-row';&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; row.style.display = 'none';&nbsp; &nbsp; }&nbsp; })})

郎朗坤

const search = document.querySelector("input[name=search]");const table = document.querySelector("#myTable");const rows = table.querySelectorAll("tr");search.addEventListener("input", () => {&nbsp; rows.forEach(row => {&nbsp; &nbsp; const matches = row.textContent&nbsp; &nbsp; &nbsp; .toLowerCase()&nbsp; &nbsp; &nbsp; .includes(search.value.toLowerCase())&nbsp; &nbsp; matches ? row.classList.remove("hide") : row.classList.add("hide");&nbsp; });});* {&nbsp; box-sizing: border-box;}.hide {&nbsp; display: none !important;}input[name=search] {&nbsp; background-image: url('/css/searchicon.png');&nbsp; background-position: 10px 10px;&nbsp; background-repeat: no-repeat;&nbsp; width: 100%;&nbsp; font-size: 16px;&nbsp; padding: 12px 20px 12px 40px;&nbsp; border: 1px solid #ddd;&nbsp; margin-bottom: 12px;}#myTable {&nbsp; border-collapse: collapse;&nbsp; width: 100%;&nbsp; border: 1px solid #ddd;&nbsp; font-size: 18px;}#myTable th,#myTable td {&nbsp; text-align: left;&nbsp; padding: 12px;}#myTable tr {&nbsp; border-bottom: 1px solid #ddd;}#myTable tr.header,#myTable tr:hover {&nbsp; background-color: #f1f1f1;}<h2>My Customers</h2><input type="text" name="search" placeholder="Search for names.." title="Type in a name"><table id="myTable">&nbsp; <thead>&nbsp; &nbsp; <tr class="header">&nbsp; &nbsp; &nbsp; <th style="width:60%;">Name</th>&nbsp; &nbsp; &nbsp; <th style="width:40%;">Country</th>&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>Alfreds Futterkiste</td>&nbsp; &nbsp; &nbsp; <td>Germany</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>Berglunds snabbkop</td>&nbsp; &nbsp; &nbsp; <td>Sweden</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>Island Trading</td>&nbsp; &nbsp; &nbsp; <td>UK</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>Koniglich Essen</td>&nbsp; &nbsp; &nbsp; <td>Germany</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td><a href="https://teluguhitflopmovieslist.blogspot.com/">Laughing Bacchus Winecellars</a></td>&nbsp; &nbsp; &nbsp; <td>Canada</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>Magazzini Alimentari Riuniti</td>&nbsp; &nbsp; &nbsp; <td>Italy</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>North/South</td>&nbsp; &nbsp; &nbsp; <td><a href="https://lyricxona.blogspot.com/">UK</a></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>Paris specialites</td>&nbsp; &nbsp; &nbsp; <td>France</td>&nbsp; &nbsp; </tr>&nbsp; </tbody></table>

守着星空守着你

function myFunction() {&nbsp;var input, filter, table, tr, td, i, txtValue;&nbsp;input = document.getElementById("myInput");&nbsp;filter = input.value.toUpperCase();&nbsp;table = document.getElementById("myTable");&nbsp;tr = table.getElementsByTagName("tr");&nbsp;for (i = 0; i < tr.length; i++) {&nbsp; &nbsp;td = tr[i].getElementsByTagName("td")[0];&nbsp; &nbsp;if (td) {&nbsp; &nbsp; &nbsp;txtValue = td.textContent || td.innerText;&nbsp; &nbsp; &nbsp;if (txtValue.toUpperCase().indexOf(filter) > -1) {&nbsp; &nbsp; &nbsp; &nbsp;tr[i].style.display = "";&nbsp; &nbsp; &nbsp; &nbsp;i++;&nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp;tr[i].style.display = "none";&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp;&nbsp;&nbsp; &nbsp;td = tr[i].getElementsByTagName("td")[1];&nbsp; &nbsp;if (td) {&nbsp; &nbsp; &nbsp;txtValue = td.textContent || td.innerText;&nbsp; &nbsp; &nbsp;if (txtValue.toUpperCase().indexOf(filter) > -1) {&nbsp; &nbsp; &nbsp; &nbsp;tr[i].style.display = "";&nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp;tr[i].style.display = "none";&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}}https://jsfiddle.net/cjb1rx6f/1/这有效,但其他答案更好......

慕田峪9158850

即使您增加列数,这也应该有效基本上,您遍历所有TD内部TR,如果找到一个匹配项,则转到下一行。function myFunction() {&nbsp; var input, filter, table, tr, td, i, txtValue;&nbsp; input = document.getElementById("myInput");&nbsp; filter = input.value.toUpperCase();&nbsp; table = document.getElementById("myTable");&nbsp; tr = table.getElementsByTagName("tr");&nbsp; for (i = 0; i < tr.length; i++) {&nbsp; &nbsp; tds = tr[i].getElementsByTagName("td")&nbsp; &nbsp; var foundInRow = false&nbsp; &nbsp; for(j=0; j<tds.length; j++){&nbsp; &nbsp; if(foundInRow)&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; td = tr[i].getElementsByTagName("td")[j];&nbsp; &nbsp; if (td) {&nbsp; &nbsp; &nbsp; txtValue = td.textContent || td.innerText;&nbsp; &nbsp; &nbsp; if (txtValue.toUpperCase().indexOf(filter) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; foundInRow = true&nbsp; &nbsp; &nbsp; &nbsp; tr[i].style.display = "";&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; tr[i].style.display = "none";&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript