使用js實現前端內容即時搜尋

來源:互聯網
上載者:User

標籤:des   style   使用   io   ar   cti   代碼   html   工作   

  本文主要基於項目的一個基本搜尋功能,最上層顯示所有使用者的線上評論資訊;使用者可以

根據需要在搜尋方塊中輸入搜尋索引鍵,即時搜尋出相應的顯示結果,並高亮顯示。

實現代碼:

 /**             

* @brief 搜尋一條房間中的符合要求的發言             

* @param $keyword 搜尋的關鍵字                                    

*/            

var isSkip = false;      

$(‘.search-icon‘).click(function() {                

showSearch();            

});

function showSearch() {

  $(‘.search-content‘).addClass(‘bounce‘).slideDown(‘fast‘, function() {                    

$(‘.present-line-gray-whole‘).addClass(‘present-line-gray-whole-hide‘);                    

$(‘.search-content input‘).focus();                

});            

}

$(‘.delete-icon‘).click(function() {                

hideSearch();            

});

 function hideSearch() {                

$(‘.present-line-gray-whole‘).removeClass(‘present-line-gray-whole-hide‘);                

$(‘.search-content‘).slideUp(100, backToOrigin);                

$(‘.talk-board-wrapper‘).animate({ scrollTop: 0}, ‘slow‘);                

$(‘.search-content‘).removeClass(‘bounce‘);                

isSkip = false;            

}

$(‘.search-content input‘).keyup(function(e) {                

$(‘.no-match-chat‘).removeClass(‘no-match-chat-change‘);                

$(‘.talk-board-wrapper‘).scrollTop(0);                

if ($(‘.search-content input‘).val() != ‘‘) {                    

$(‘.search-content input‘).css(‘border-bottom‘, ‘1px solid rgb(255,209,37)‘);                    

$(‘#search-img‘).trigger(‘click‘);                

} else {                    

$(‘.search-content input‘).css(‘border-bottom‘, ‘1px solid rgb(65,65,65)‘);                    

$(‘.talk-board-container .chat-item‘).show();                    

removeLastHighlightResult();               

 }

 });

$(‘.talk-board-container‘).on(‘click‘, ‘.chat-item‘, chatItemClick);

function chatItemClick() {                

var itemIndex;                

itemIndex = $(this).index();                

if (isSkip) {                    

backToOrigin();                    

skipToDest(itemIndex);                

}                

isSkip = false;           

  }

function skipToDest(itemIndex) {                

var scrollLength;                

scrollLength = parseInt($(‘.ps-scrollbar-y-rail‘).css(‘top‘)) + $(‘.talk-board-container .chat-item‘).eq(itemIndex).offset().top - 75;                

$(‘.talk-board-wrapper‘).animate({scrollTop: scrollLength}, ‘slow‘);}

function backToOrigin() {                

$(‘.search-content input‘).val(‘‘).css(‘border-bottom‘, ‘1px solid rgb(65,65,65)‘);                

$(‘.talk-board-container .chat-item‘).show();                

$(‘.no-match-chat‘).removeClass(‘no-match-chat-change‘);                

removeLastHighlightResult();            

}

 $(‘#search-img‘).on(‘click‘, function() {                

isSkip = true;                

$(‘.talk-board-container .chat-item‘).hide();                

removeLastHighlightResult();                

markSearchResult();            

});

function removeLastHighlightResult() {                

var $searchElement,                   

appendText;                

$searchElement = $(‘.chat-content p‘).find(‘i‘).length ?                    

$(‘.chat-content p‘).find(‘i‘) : ‘‘;               

if ($searchElement.length) {                     //刪除上一次匹配並高亮顯示的結果                    

$.each($searchElement, function(index, content) {                        

appendText = $(content).text();                        

$(content).after(appendText).remove();                    

});                

}            

}

function markSearchResult() {                

var searchResult = false,                    

keyword = $(‘#search-content-input‘).val(),                    

matchStr,                    

$a,                    

$span,                    

$chatContent;                

$chatContent = $(‘.chat-content p‘);                

if (keyword === ‘‘) {                    

$(‘.no-match-chat‘).addClass(‘no-match-chat-change‘);                    

return;                

}                

$.each($chatContent, function(index, content) {                     //遍曆整個chat內容,尋找到匹配的字串高亮顯示                    

if ($(content).text().indexOf(keyword, 0) !== -1) {                        

$(content).parents(‘.chat-item‘).show();                        

searchResult = ‘true‘;                        

if ($(content).find(‘a‘).length) {                            

matchStr = $(content).find(‘a‘).contents().filter(function() {                                

return this.nodeType == 3;                            

}).text().replace(new RegExp(keyword, ‘g‘), ‘<i class="text-background-highlight">‘ + keyword + ‘</i>‘);                            

$a=$(content).find(‘a‘).clone();                            

$span = $(content).find(‘a span‘).clone();                            

$(content).html($(content).contents().filter(function() {                                

return this.nodeType == 3;                            

}).text().replace(new RegExp(keyword, ‘g‘), ‘<i class="text-background-highlight">‘ + keyword + ‘</i>‘));                            

$(content).prepend($a);                            

$(content).find(‘a‘).html(matchStr);                            

$(content).find(‘a‘).prepend($span);                        

} else {                            

$(content).html(                                

$(content).text().replace(new RegExp(keyword, ‘g‘), ‘<i class="text-background-highlight">‘ + keyword + ‘</i>‘) );                        

}                    

}                

});                

if (!searchResult) {                    

$(‘.no-match-chat‘).addClass(‘no-match-chat-change‘);                

}            

}

            /* 搜尋工作結束 */

使用js實現前端內容即時搜尋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.