2013-04-08 41 views
0

我需要一个功能,我必须突出显示/替换文章正文的匹配标记/关键字,并将这些匹配的标记/关键字转换为链接下面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> 
+0

我用我现在用的是这个例子http://jsfiddle.net/LE3sg/3/ – Learning 2013-04-08 13:51:36

回答

0

您正在传递一个字符串和数组的函数。

尝试使用:

// array format = ['one', 'two','three','US','UK']; 

,而不是

// array format = " 'one', 'two','three','US','UK' "; 
+0

为'['一个','two','three','US','UK'];'在实际代码中c#以正确的格式将数组返回给jQuery。如果它与任何数组元素相匹配,问题就在于它取代了病房的一部分。 – Learning 2013-04-09 03:45:54