dom 検索/置換スクリプトを変更して、それに一致する複数のキーワードをドキュメント内のリンクで置換しました。
<div>
またはなしでうまく機能<p>
していましたが、複雑な構造で各ノードのキーワードが置き換えられています...
これは例です
ご覧のとおり、要素内で同じキーワードが複数回リンクされているわけではありませんが、他の要素がいくつかある間にキーワードがリンクされています...
これがスクリプトです
(function(){
// don't replace text within these tags
var skipTags = { 'a': 1, 'style': 1, 'script': 1, 'iframe': 1, 'meta':1, 'title':1, 'img':1, 'h1':1 };
// find text nodes to apply replFn to
function findKW( el, term, replFn )
{
var child, tag;
if(!found)var found=false;
for (var i = 0;i<=el.childNodes.length - 1 && !found; i++)
{
child = el.childNodes[i];
if (child.nodeType == 1)
{ // ELEMENT_NODE
tag = child.nodeName.toLowerCase();
if (!(tag in skipTags))
{
findKW(child, term, replFn);
}
}
else if (child.nodeType == 3)
{ // TEXT_NODE
found=replaceKW(child, term, replFn);
}
}
};
// replace terms in text according to replFn
function replaceKW( text, term, replFn)
{
var match,
matches = [],found=false;
while (match = term.exec(text.data))
{
matches.push(match);
}
for (var i = 0;i<=matches.length - 1 && !found; i++)
{
match = matches[i];
// cut out the text node to replace
text.splitText(match.index);
text.nextSibling.splitText(match[1].length);
text.parentNode.replaceChild(replFn(match[1]), text.nextSibling);
if(matches[i])found=true;// To stop the loop
}
return found;
};
// Keywords to replace by a link
var terms=Array('keywords','words');
for(kw in terms)
{
findKW(
document.body,
new RegExp('\\b(' + terms[kw] + ')\\b', 'gi'),
function (match)
{
var link = document.createElement('a');
link.href = 'http://www.okisurf.com/#q=' + terms[kw];
link.id = '1';
link.target = '_blank';
link.innerHTML = match;
return link;
}
);
}
}());
誰かがループを止めて、最初のキーワード一致のみを置き換えるのを手伝ってくれませんか? (私はそれらのノードとvar found
、関数のためにスレッドがループで動作している間、グローバルのように送信できないことに夢中になっていfindKW()
ます...)そして、ライブラリなし(jQueryまたはその他なし)