返回 jQuery 实例与特效代码列表
表格与数据MIT兼容 jQuery 1.12 / 3.8 / 4.1
表格搜索过滤
输入关键词实时过滤表格行,无匹配时自动展示空态提示并实时统计命中条数。
兼容性说明:全版本兼容(含 slim 精简版)。
在线交互演示(独立沙箱)
$(function () {
$('#tf-input').on('input', function () {
var q = String($(this).val() || '').trim().toLowerCase();
var matched = 0;
$('#tf-body tr').each(function () {
var ok = !q || $(this).text().toLowerCase().indexOf(q) !== -1;
$(this).toggle(ok);
if (ok) matched++;
});
$('#tf-count').text('命中 ' + matched + ' 条');
$('#tf-empty').toggle(matched === 0);
});
});不用 jQuery 怎么写?(现代原生 JS / CSS 替代思路)
监听 input 事件遍历 tbody.rows,通过 row.hidden = !row.textContent.toLowerCase().includes(q) 切换显示。
input.addEventListener('input', () => {
const q = input.value.trim().toLowerCase();
for (const r of tbody.rows) r.hidden = !r.textContent.toLowerCase().includes(q);
});