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

发布于 2024-12-07 00:59:44 字数 517 浏览 0 评论 0原文

我想使用 jQuery 将特定单词(laptop、asus、acer)替换为链接(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>

,但问题是它替换了现有网址中的笔记本电脑单词(如果网址是 test.com/laptop-review,则创建像 test.com/test.com/laptop-review 这样的网址)和所有笔记本电脑 html (html页面上的 ids、classes、url 等)被破坏。

谢谢

I want to replace specific words (laptop, asus, acer) with links (test.com/l12, test.com/13, test.com/14) using jQuery.
I found this function

<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>

but the problem is that it replaces laptop word in existing urls (creating urls like test.com/test.com/laptop-review if the url was test.com/laptop-review) and all the laptop html (html ids, classes, urls etc) on the page is destroyed.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野却迷人 2024-12-14 00:59:44

你可以这样做:

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

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


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

fiddle http://jsfiddle.net/LXw6P/

编辑当然你可以使它成为一个函数并重用它:

var updateTxt = function(stringToReplace){
    var replaceText = '<a href="http://test.com/'+stringToReplace'+">'+stringToReplace+'</a>';
    $('body *').each(function(){
       var txt = $(this).text();

        txt =  txt.replace(stringToReplace, replaceText);


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

使用它:

updateTxt('laptop');

You could do:

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

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


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

fiddle http://jsfiddle.net/LXw6P/

EDIT of course you could make this a function and reuse it:

var updateTxt = function(stringToReplace){
    var replaceText = '<a href="http://test.com/'+stringToReplace'+">'+stringToReplace+'</a>';
    $('body *').each(function(){
       var txt = $(this).text();

        txt =  txt.replace(stringToReplace, replaceText);


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

use it:

updateTxt('laptop');
夏日浅笑〃 2024-12-14 00:59:44

您可以尝试使用 jQuery 文本替换插件 - http:// /net.tutsplus.com/tutorials/javascript-ajax/spotlight-jquery-replacetext/

该插件应忽略 HTML 标记并仅更改“文本”值。

此问题中给出了更多详细信息 - 如何对单词进行不区分大小写的搜索并使用 jquery 将其转换为超链接?

You could try using the jQuery text replace plug-in - http://net.tutsplus.com/tutorials/javascript-ajax/spotlight-jquery-replacetext/

The plug-in should ignore HTML mark-up and only change 'text' values.

Further details are given in this question - How do I do a case insensitive search for a word and convert it to a hyperlink using jquery?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文