我正在开发一个包含实时搜索的网站。此实时搜索用于搜索联系人列表中的联系人(HTML表格)。联系人列表是一个包含2列的表格,每列包含一个联系人。搜索工作,但它返回整行,而不仅仅是匹配的列。通过HTML表格列搜索
这意味着如果我在下面的代码片段中查找表格中的A;搜索返回整行(A || B),而不仅仅是A.有没有什么办法可以改进我的函数来搜索列而不是行?
希望我解释清楚。
<table>
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>C</td>
<td>D</td>
</tr>
</table>
功能
<script>
function myFunction() {
//variables
var input, filter, table, tr, td, i;
input = document.getElementById("search");
filter = input.value.toUpperCase();
table = document.getElementById("table");
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 = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
要设置'display'到'none'或' '''为全行'TR [I] .style.display = “none”;' – xxxmatko
此外,您的示例缺少搜索输入。 – xxxmatko