我正在尝试插入搜索函数进入我的网站,我发现的一个可以从多个列找到数据(这是伟大的),但问题是,在搜索结果显示后,文本标题不显示(这并不好)。有人知道如何显示搜索结果,包括文本标题吗?对不起,我还是个编程新手。
<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>
您需要跳过第一行(即标题行)
从1
开始循环for (i = 1; i < tr.length; i++) {
你的表结构应该是这样的
<table>
<thead>
<tr></tr>
</thead>
<tbody>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</tbody>
</table>
如果你的表是这样的,那么你只能像这样修改一行:
tr = document.querySelector('tbody').getElementsByTagName("tr");
table = document.getElementById("T_SJSM");
添加"T_SJSM"Id到主体
或从1开始第一个循环(tr循环)。