我调用post-content了多个类,div并有一个搜索框,该事件被触发keyup。如果没有匹配的结果,我想显示' 没有结果匹配 '
$('.post-content').each(function() {
$(this).attr('data-search-term', $(this).text().toLowerCase());
});
$('#search-criteria').on('keyup', function() {
var searchTerm = $(this).val().toLowerCase();
$('.post-content').each(function() {
if ($(this).filter('[data-search-term *= ' + searchTerm + ']').length > 0) {
$(this).show();
} else {
$(this).hide();
}
});
});
Run Code Online (Sandbox Code Playgroud)
我尝试在else块内创建该元素,但是似乎每次按下该键时都会触发,从而打印出带有事件的整个页面。