【问题标题】:string search in body.html() not workingbody.html() 中的字符串搜索不起作用
【发布时间】: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


【解决方案1】:

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

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

Try this amended fiddle

【讨论】:

  • 这将在 IE 点击 Ctrl+V...
猜你喜欢
  • 2013-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
相关资源
最近更新 更多