如何使用jQuery将单词替换为url/url?

如何使用jQuery将单词替换为url/url?,jquery,url,replace,word,words,Jquery,Url,Replace,Word,Words,我想用jQuery的链接(test.com/l12、test.com/13、test.com/14)替换特定的单词(膝上型电脑、华硕、宏碁)。 我找到了这个函数 <script type="text/javascript"> (function($) { var thePage = $("body"); thePage.text(thePage.html().replace(/laptop/ig, '<a href="http://tes

我想用jQuery的链接(test.com/l12、test.com/13、test.com/14)替换特定的单词(膝上型电脑、华硕、宏碁)。 我找到了这个函数

<script type="text/javascript">
(function($) {
          var thePage = $("body");
          thePage.text(thePage.html().replace(/laptop/ig, '<a href="http://test.com/laptop">laptop</a>')); 
})(jQuery)
</script>

(函数($){
var thePage=$(“主体”);
text(thePage.html().replace(/laptop/ig');
})(jQuery)
但问题是,它替换了现有url中的laptop word(如果url是test.com/laptop review,则创建类似test.com/test.com/laptop-review的url),页面上的所有笔记本html(html ID、类、url等)都被销毁

谢谢你

你可以做:

$('body *').each(function(){
     var txt = $(this).text();

    txt =  txt.replace('laptop', '<a href="http://test.com/laptop">laptop</a>');


    $(this).html(txt);
});
你可以做:

$('body *').each(function(){
     var txt = $(this).text();

    txt =  txt.replace('laptop', '<a href="http://test.com/laptop">laptop</a>');


    $(this).html(txt);
});

您可以尝试使用jQuery文本替换插件-

插件应该忽略HTML标记,只更改“文本”值


此问题中提供了更多详细信息-

您可以尝试使用jQuery文本替换插件-

插件应该忽略HTML标记,只更改“文本”值


进一步的细节在这个问题中给出了-

替换(/^laptop/ig,
),或者你可以尝试
替换(/laptop\s/ig,
),所以任何跟在空格后面的单词作为URL都不应该包含空格。我会尝试
替换(/^laptop/ig,
),或者你可以尝试
替换(/laptop\s/ig,
因此,任何后跟空格作为URL的单词都不应该包含空格。我想都不应该。我会尝试这样做,这很好,但是循环遍历每个元素是一项累人的任务。:)也许在特殊单词中添加一个特殊的类和跨度可以减轻工作量:)这很好,但是循环遍历每个元素是一项累人的任务。:)也许在特殊单词中添加一个特殊的类和跨度可以减轻工作量:)
updateTxt('laptop');