我正在尝试添加一条消息,如果某人的搜索结果为空,我可以显示该消息。类似于"这里似乎什么都没有。"
我正在使用Isotope.JS来处理我的过滤,并试图使用基于长度的JS If/Else语句来显示此错误消息。我似乎无法让它发挥作用,因为控制台显示[0]
对象一直在显示,即使没有搜索到。
错误消息的我的JS函数
var exampleCount = $('.examples-container .example').length;
//shows empty state text when no jobs found
if(exampleCount == '0') {
$('.examples-container').addClass('empty');
}
else {
$('.examples-container').removeClass('empty');
}
正在添加和删除的类
.empty-item {
transition-property: opacity;
transition-duration: 0s;
transition-delay: 0s;
transition-timing-function: ease;
display:none;
}
.empty .empty-item {
transition-property: opacity;
transition-duration: .2s;
transition-delay: .3s;
transition-timing-function: ease;
display:block !important;
}
我的div将被隐藏/显示
<span class="empty-item">no results</span>
您可以在代码笔中看到一个示例。它似乎不想附加我的类,因为它无法在examples-container
中获得我的示例的长度。
好吧,所以我找到了我的答案。基本上,我需要在keyup
或搜索函数的末尾有我的if/else。这就是它的样子:
// use value of search field to filter
var $quicksearch = $('#quicksearch').keyup( debounce( function() {
qsRegex = new RegExp( $quicksearch.val(), 'gi' );
$container.isotope();
// display message box if no filtered items
if ( !$container.data('isotope').filteredItems.length ) {
$('#noResult').show();
} else {
$('#noResult').hide();
}
}) );
然后我交换了HTML,使id
更易于管理,所以我将其用于"无结果"div
。
<div id="noResult">
<div>No results</div>
</div>
这是我的最终结果。