我正在从页面中搜索文本。一切都很好。但是只有一个问题,当用户搜索&它搜索时,还会用amp附加数据附加数据。你能告诉我为什么会发生吗?
这是我的小提琴:http: //jsfiddle.net/ravi1989/wjLmx/22/
请输入 & ,然后点击搜索,再次点击搜索,搜索,下一步查找问题。
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
//Remove the previous matches
$span = $('#realTimeContents span');
$span.replaceWith($span.html());
$(selector).html($(selector).html().replace(searchTermRegEx, "<span class='match'>" + searchTerm + "</span>"));
$('.match:first').addClass('highlighted');
var i=0;
$('.next_h').off('click').on('click', function () {
i++;
if(i >= $('.match').length)
i=0;
$('.match').removeClass('highlighted');
$('.match').eq(i).addClass('highlighted');
});
$('.previous_h').off('click').on('click', function () {
i--;
if(i < 0)
i=$('.match').length-1;
$('.match').removeClass('highlighted');
$('.match').eq(i).addClass('highlighted');
});
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");
}
});