body.html()中的字符串搜索不起作用

时间:2011-11-23 11:46:55

标签: javascript jquery search full-text-search

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标签匹配..

小提琴第二段不匹配。

1 个答案:

答案 0 :(得分:1)

首先,要忽略要查看的元素的HTML标记,请使用.text()方法。

其次,在你的小提琴中,它没有用,因为你没有在加载时调用SearchQueue函数。

Try this amended fiddle