【发布时间】:2012-01-04 17:07:54
【问题描述】:
嗨 here 是我在 HTML 中搜索字符串并在文档中找到它时突出显示它的全部工作: 问题就在这里
var SearchItems = text.split(/\r\n|\r|\n/);
var replaced = body.html();
for(var i=0;i<SearchItems.length;i++)
{
var tempRep= '<span class="highlight" style="background-color: yellow">';
tempRep = tempRep + SearchItems[i];
tempRep = tempRep + '</span>';
replaced = replaced.replace(SearchItems[i],tempRep); // It is trying to match along with html tags...
// As the <b> tags will not be there in search text, it is not matching...
}
$("body").html(replaced);
我使用的HTML如下;
<div>
The clipboardData object is reserved for editing actions performed through the Edit menu, shortcut menus, and shortcut keys. It transfers information using the system clipboard, and retains it until data from the next editing operation replace s it. This form of data transfer is particularly suited to multiple pastes of the same data.
<br><br>
This object is available in script as of <b>Microsoft Internet Explorer 5.</b>
</div>
<div class='b'></div>
如果我搜索一个纯页面或没有任何 html 标签的页面,它将匹配。但是,如果我在 HTML 中有任何标签,这将不起作用。因为我将 body html() 文本作为目标文本。它正试图与 html 标签匹配..
小提琴第二段将不匹配。
【问题讨论】:
-
请在问题中包含所有相关代码in,以及问题中的链接。问题应该独立于其他任何事情。此外,众所周知,JsFiddle 不可靠。
标签: javascript jquery search full-text-search