검색 기능을 구현 중입니다. 링크가 잘 작동하고 있습니다.하지만 한 가지 문제는있을 때 알림이 설정되지 않았기 때문입니다. 페이지에서 일치하는 항목을 찾을 수 없습니다. 알림을 삽입하는 위치를 알려주십시오. 경고가 표시되지 않습니다. 나는 그런 식으로 끝냈다. .match.length() == 0 경고 표시하지만 작동하지 않는다면 도움을받을 수 있습니까?jquery의 검색 결과와 일치하는 항목이없는 경우 경고를 설정하는 방법
여기 코드를 발견 한 링크입니다. 그게 사실 반환하는 경우 http://jsbin.com/umodoy/7/edit
var searchIndex = -1;
var searchTermOld ='';
$(document).ready(function(){
$('.searchbox').on('change',function(){
if($(this).val()===''){
var selector= "#realTimeContents";
$(selector+' span.match').each(function(){
$(this).replaceWith($(this).html());
});
}
searchIndex = -1;
$('.searchNext').attr("disabled", "disabled");
$('.searchPrev').attr("disabled", "disabled");
searchTermOld = $(this).val();
});
$('.searchbox').on('keyup',function(){
var selector= "#realTimeContents";
if($(this).val()===''){
$(selector+' span.match').each(function(){
$(this).replaceWith($(this).html());
});
}
if($(this).val() !== searchTermOld){
$(selector+' span.match').each(function(){
$(this).replaceWith($(this).html());
});
searchIndex = -1;
$('.searchNext').attr("disabled", "disabled");
$('.searchPrev').attr("disabled", "disabled");
}
});
$('.search').on('click',function(){
if(searchIndex == -1){
var searchTerm = $('.searchbox').val();
searchAndHighlight(searchTerm);
}
else searchNext();
if($('.match').length >1){
$('.searchNext').removeAttr("disabled");
$('.searchPrev').removeAttr("disabled");
}
});
$('.searchNext').on('click',searchNext);
$('.searchPrev').on('click',searchPrev);
});
function searchAndHighlight(searchTerm) {
if (searchTerm) {
var searchTermRegEx, matches;
var selector= "#realTimeContents";
$(selector+' span.match').each(function(){
$(this).replaceWith($(this).html());
});
try {
searchTermRegEx = new RegExp('('+searchTerm+')', "ig");
} catch (e) {
return false;
}
$('.highlighted').removeClass('highlighted');
matches = $(selector).text().match(searchTermRegEx);
if (matches !==null && matches.length > 0) {
var txt = $(selector).text().replace(searchTermRegEx, '<span class="match">$1</span>');
$(selector).html(txt);
searchIndex++;
$('.match:first').addClass('highlighted');
$(document).scrollTop($('.match').eq(searchIndex).offset().top);
return true;
}
return false;
}
return false;
}
function searchNext(){
searchIndex++;
if (searchIndex >= $('.match').length) searchIndex = 0;
$('.highlighted').removeClass('highlighted');
$('.match').eq(searchIndex).addClass('highlighted');
$(document).scrollTop($('.match').eq(searchIndex).offset().top);
}
function searchPrev(){
searchIndex--;
if (searchIndex < 0) searchIndex = $('.match').length - 1;
$('.highlighted').removeClass('highlighted');
$('.match').eq(searchIndex).addClass('highlighted');
$(document).scrollTop($('.match').eq(searchIndex).offset().top);
}
는
만큼 많이 스크롤됩니다. 그것도 스크롤 같은 페이지에서 일치합니다. 나는 그것만 아래 또는 위의 페이지를 – Rohit
확인해 보자 .. –
당신이 먼저 큰 데이터를 추가 한 다음 그것을 너무 많이 스크롤 스크롤 텍스트를 스크롤 좋지 검색 .. – Rohit