【发布时间】:2019-01-21 02:21:55
【问题描述】:
如果您尝试使用 jQuery 根据输入值隐藏和显示子项,则会在键入时导致性能问题。为避免在每个字符后调用过滤器函数,请使用lodash 的debounce 方法。
html
<input id="search" />
<div>
<div class="child">foo</div>
<div class="child">bar</div>
....
</div>
java脚本
var filterChildren = function() {
var value = $(this).val().toLowerCase();
$(".child").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
});
};
$("#search").on("keyup", _.debounce(filterChildren, 300));
PS:这是这篇文章的答案的一部分: How to hide the parent div if all its child div are hidden?
【问题讨论】:
标签: javascript jquery lodash