我需要一個功能,我必須突出顯示/替換文章正文的匹配標記/關鍵字,並將這些匹配的標記/關鍵字轉換爲鏈接下面jQuery函數用鏈接標記替換匹配的標記/關鍵字
en/search.aspx?language=en-US&issue=1&pageID=2&search=Something
下面的代碼,我使用的,除非它與關鍵字匹配,它甚至改變了HTML精品工程。
邏輯爲下面的代碼是簡單的I傳遞數組從代碼後面的jquery功能在這種情況下,下面的格式"[ 'one', 'two','three','US','UK' ]"
它會改變以上的鏈接也可作爲烯US
匹配而循環的數組元素的一個通過內容N無次
我希望幫助在這方面,使函數只改變的話不是單詞的任何匹配的部分,而忽略HTML標記,而做相同
部分代碼在ASP.Net格式如<%= _pPID%>;
function HighlightKeywords(keywords)
{
var el = $("#article-detail-desc");
var language = "<%= _planguage %>";
var pid = <%= _pPID %>;
var issueID = <%= _pIssue %>;
// array format = " 'one', 'two','three','US','UK' ";
$(keywords).each(function()
{
var pattern = new RegExp("("+this+")", ["gi"]);
var rs = "<a class='ad-keyword-selected' href='en/search.aspx?Language="+language+"&PageId="+pid+"&issue="+issueID+"&search=$1' title='Seach website for: $1'><span style='color:#990044; tex-decoration:none;'>$1</span></a>";
el.html(el.html().replace(pattern, rs));
});
}
HighlightKeywords([<%= _pKeywords %>]);
<div id="article-detail-desc">
all the text related to article will be show displayed inside this div and any matching words will be replaced by the link <a></a>
</div>
我用我現在用的是這個例子http://jsfiddle.net/LE3sg/3/ – Learning 2013-04-08 13:51:36