좋은 전략은 다음과 같습니다
1) 그 키 값을 대체 할 수있는 링크가있는 대체 할 문구와이 객체를 구축합니다.
2) 그 일을하는 동안, 다음 키 중 하나와 일치 할 수있는 정규 표현식,
3) 대체 전역 해당 정규식을 사용을 구성.
거친 예 :
이
<div class="message">Somethsg1</div>
<div class="message">Something</div>
<div class="message">Ssething</div>
<div class="message">Something Else</div>
<div class="message">Something da</div>
<div class="message">Somethin2g</div>
당신은 같은 것을 사용할 수 있습니다 :
//your array
var words = [
{
word: 'Something',
link: 'http://www.something.com'
},
{
word: 'Something Else',
link: 'http://www.something.com/else'
}
];
//iterate the array
$.each(words,
function() {
//find an element with class "message" that contains "word" (from array)
$('.message:contains("' + this.word + '")')
//substitute html with a nice anchor tag
.html('<a href="' + this.link + '">' + this.link + '</a>');
}
);
을이 솔루션은 하나의 즉각적인 문제를 생각합니다 (에 보여왔다
var replacementDict = {
'foo': 'http://www.foo.com/',
'bar': 'http://www.bar.net/'
};
var theRegex = /\b(foo|bar)\b/g;
theText.replace(theRegex, function(s, theWord) {
return "<a href='" + replacementDict[theWord] + "'>" + theWord + "</a>";
});
서버 측으로 옮길 것을 제안합니다. – LiraNuna
음 .. CPU Hungry? 간단한 텍스트 대체? 그리고, 당신이 다루고있는 요소, 구조, 당신이 알고있는 .. 평소와 같이, 그 페이지에 대한 배경 정보를 더 제공해야합니다. –
@LiraNuna : 불행히도. @meder : 추가 정보를 추가했습니다. –