How to turn keyword on a site into links with jQuery

前端 未结 2 1955
-上瘾入骨i
-上瘾入骨i 2021-01-25 05:49

I\'m trying to turn a set of keywords into links on my site. I am currently using this code which will turn one keyword into a link. However, now I want to expand it to have sev

2条回答
  •  情深已故
    2021-01-25 06:33

    I would suggest something like this:

    var keywordsArray = ["place", "all", "of the", "keywords in here like this"];
    var thePage = $("body");
    
    for (var i = 0; i < keywordsArray.length; i++) {
        thePage.find(":contains("+keywordsArray[i]+")").each(function(){
            var _this = $(this);
            var content = _this.html();
            content.replace(keywordsArray[i], '' + keywordsArray[i] + '');
    
            _this.html(content);
        });
    };
    

提交回复
热议问题