【发布时间】:2017-02-03 23:40:58
【问题描述】:
在搜索框中搜索任何文本时,可以找到并突出显示正确的文本,但是当搜索下一个/新文本时,无法找到下一个/新文本,再次搜索时它不起作用,我无法找到问题。下面的JS。
JS
$('button#search').click(function() {
var page = $('#ray-all_text');
var pageText = page.html().replace("<span>", "").replace("</span>");
var searchedText = $('#searchfor').val();
var theRegEx = new RegExp("(" + searchedText + ")", "igm");
var newHtml = pageText.replace(theRegEx, "<span>$1</span>");
page.html(newHtml);
$('html, body').animate({
scrollTop: $("#ray-all_text span").offset().top }, 2000);
});
HTML
<div class="ray-search">
<div class="field" id="ray-search-form">
<input type="text" id="searchfor" placeholder="what are you searching for?" />
<button type="button" id="search">Press to Find!</button>
</div>
</div>
<article id="ray-all_text">
<p>
This manual and web site, all information and data and photos contained herein, are the s...
</p>
</article>
请查看实时示例:https://jsfiddle.net/gaezs6s8
为什么会这样?有解决办法吗?
【问题讨论】:
-
为什么都在 settimeout 函数中?
-
是的,js 在 1 秒后加载,我需要它在 1 秒后加载。有什么解决办法吗?
标签: javascript jquery html regex search