我正在研究搜索功能,并希望在四个选项卡(选项卡窗格)中的每一个中显示信息“无搜索结果”。而是多次显示“无搜索结果”消息。每次创建 div 之前,我都会删除它。见小提琴。
HTML结构是这样的:
<div class="tab-content">
<div class="tab-pane active" id="Incoming">
<div class="accordion" id="accordionIncoming"> </div>
</div>
<div class="tab-pane" id="Escalation">
<div class="accordion" id="accordionEscalation"> </div>
</div>
<div class="tab-pane" id="Outgoing">
<div class="accordion" id="accordionOutgoing"> </div>
</div>
<div class="tab-pane" id="Manual-tasks">
<div class="accordion" id="accordionManual-tasks"> </div>
</div>
</div>
相关的javascript是这样的:
...
else if (searchTerm !== "" && countNoResults === 0 && v.indexOf(searchTerm) === -1){
$('#well-exists').remove();
$('.tab-pane').append('<div class="well well-small" id="well-exists">No search results.</div>');
}
更新:我通过使用 show/hide 解决了这个问题。添加到选项卡内容的 HTML:
<div class="tab-content">
<div class="well well-small" id="well-exists" style="display:none;">No search results. </div>
和javascript:
$.each(cachedText, function (i, v) {
if (searchTerm.length > 0 && v.indexOf(searchTerm) !== -1) {
$infoPanels.eq(i).show().parentsUntil('.accordion').addClass("in").show();
$('.accordion-body').addClass("in").css('height', 'auto');
countNoResults++;
if (v.indexOf(searchTerm) !== -1) {
$('#well-exists').hide();
}
}
else if (searchTerm !== "" && countNoResults === 0 && v.indexOf(searchTerm) === -1){
console.log("show");
$('#well-exists').show();
}
});