从div(page)中搜索文本将使用查询第二次崩溃 [英] Search a text from div(page) will crash second time using query
问题描述
在突出显示搜索词之后,我正在实现搜索功能.实际上,我还实现了下一个和上一个功能.实际上,当我运行它时,正确搜索该词,然后用户搜索下一个和上一个(精确).但是,当用户第二次搜索时,它也会突出显示第一个单词.在这里,我正在实现此功能. http://jsfiddle.net/ravi1989/wjLmx/2/
如果检查错误:
首先搜索:sd,然后继续.再次搜索df.您还会发现它突出显示第一个单词.
I am implementing the search functionality after search word is highlighted .Actually I also implement next and previous functionality .Actually when i run it search the word correctly and user go next and previous (finely). But when user search second time it highlight the first word also. Here i am implementing this functionality. http://jsfiddle.net/ravi1989/wjLmx/2/
If you check error :
First search : sd then go next . again search df. You find it highlight the first word also.
function searchAndHighlight(searchTerm, selector) {
if(searchTerm) {
//var wholeWordOnly = new RegExp("\\g"+searchTerm+"\\g","ig"); //matches whole word only
//var anyCharacter = new RegExp("\\g["+searchTerm+"]\\g","ig"); //matches any word with any of search chars characters
var selector = selector || "#realTimeContents"; //use body as selector if none provided
var searchTermRegEx = new RegExp(searchTerm,"ig");
var matches = $(selector).text().match(searchTermRegEx);
if(matches!=null&&matches.length>0) {
$('.highlighted').removeClass('highlighted'); //Remove old search highlights
$(selector).html($(selector).html().replace(searchTermRegEx, "<span class='match'>"+searchTerm+"</span>"));
$('.match:first').addClass('highlighted');
$('.next_h').on('click',i=1, function()
{
$('.match').removeClass('highlighted');
$('.match').eq(i).addClass('highlighted');
i=i+1;
k=i-2;
});
$('.previous_h').on('click',k=1, function()
{
if(matches.length>k&&k>0){
$('.match').removeClass('highlighted');
$('.match').eq(k).addClass('highlighted');
k=k-1;
i=k+2;
}
});
if($('.highlighted:first').length) { //if match found, scroll to where the first one appears
$(window).scrollTop($('.highlighted:first').position().top);
}
return true;
}
}
return false;
}
$(document).on('click', '.searchButtonClickText_h', function(event) {
$(".highlighted").removeClass("highlighted").removeClass("match");
if(!searchAndHighlight($('.textSearchvalue_h').val())) {
alert("No results found");
}
});
推荐答案
(selector).text().match(searchTermRegEx); 如果(matches!= null&& matches.length> 0){
(selector).text().match(searchTermRegEx); if(matches!=null&&matches.length>0) {
(.highlighted').removeClass(); // 删除旧的搜索突出显示
('.highlighted').removeClass('highlighted'); //Remove old search highlights
(选择器).html (
(selector).html(
这篇关于从div(page)中搜索文本将使用查询第二次崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!