2011-11-23 64 views
1

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

請包括** **所有相關的代碼** **中的問題,以及在問題中鏈接到它。問題應該獨立於其他任何方面。此外,JsFiddle被認爲是不可靠的。 – Matt

回答

1

首先,要忽略要查看的元素的HTML標記,請使用.text()方法。

其次,在你的小提琴中,它不起作用,因爲你沒有在加載時調用SearchQueue函數。

Try this amended fiddle

+0

這將在IE瀏覽器上點擊Ctrl + V ...單擊代碼將獲得剪貼板文本,並將搜索正文html中的字符串。 – Exception