我正在嘗試在我的網站中插入一個搜索功能,我發現的那個可以從多列中找到資料(這很棒)但問題是在顯示搜索結果后,文本標題沒有顯示(這是不好)。有誰知道如何顯示包括文本標題在內的搜索結果?我還是編碼新手,抱歉。
<script>
function tableSearch() {
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("T_SJSM");
tr = table.getElementsByTagName("tr");
// Loop through all table rows AND COLUMNS, and hide those who don't match the search query
for (i = 0; i < tr.length; i ) {
td = tr[i].getElementsByTagName("td");
var match = false;
//Loop trough all columns
for(var j = 0; j < td.length; j ) {
if(td[j]) {
txtValue = td.textContent || td.innerText;
if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
match = true;
}
}
}
//Match found in one or multiple columns
if (match) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
</script>
uj5u.com熱心網友回復:
您需要跳過第一行(即標題行)
所以從 1 開始你的回圈
for (i = 1; i < tr.length; i ) {
uj5u.com熱心網友回復:
你的表結構應該是這樣的
<table>
<thead>
<tr></tr>
</thead>
<tbody>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</tbody>
</table>
如果您的表格看起來像這樣,那么您只能像這樣更改一行:
tr = document.querySelector('tbody').getElementsByTagName("tr");
uj5u.com熱心網友回復:
表 = document.getElementById("T_SJSM");
將“T_SJSM”ID 添加到 tbody
或從 1 開始第一個回圈(tr 回圈)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/477630.html
標籤:javascript
