javascript - 用JS隐藏标题过滤HTML表格
问题描述
我目前有一个 HTML 表格的过滤器,示例如下。这也过滤了标题。
<div class="col-md-3 col-md-offset-3" style="padding-bottom: 20px;">
<input type="text" class="form-control" id="Search" onkeyup="SearchBar()" placeholder="Search">
</div>
<table id="MainTable" class="table table-condensed table-hover">
<thead>
<th width="2%" style="text-align: center;"></th>
<th width="50%">Display Name</th>
<th width="20%">Dashboard</th>
</thead>
<tbody>
<tr class="">
<td style="text-align: center;">a</td>
<td>DashboardTest</td>
<td>b</td>
</tr>
</tbody>
</table>
function SearchBar() {
var $rows = $('#MainTable tr');
$('#Search').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
$rows.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
}
有没有办法使用这个JS不包含thead?
解决方案
是的。您只能在 TBODY 中搜索。给 TBODY 一个 id="MyBody" 将此行更改为: var $rows = $('#MyBody tr'); 它不会再在 thead 中搜索了。
推荐阅读
- html - 使项目更大而不会碰到 html 中的其他项目
- java - Jetty 9 客户端设置 Content-Range 值正常不起作用
- angular - Angular 9:不订阅其他组件?
- python - 将通过列的函数,如果数字大于 0 返回列名,但当为 0 时返回“不可用”
- r - 在 R 中使用 Plotly 绘制显示毫秒的时间戳
- x86-64 - 在这个简单的例子中调用汇编指令做什么?
- node.js - API中的Put方法总是收到空的请求体
- unity3d - 如何检查 Unity 中 NavMeshAgent 的路径是否存在到 NavMesh 上的某个点?
- csv - spark.read.format('csv') 中的数据帧不正确
- java - 使用 Spring Boot 请求 Wrapper Body