Search For Words, Replace With Links

后端 未结 4 1263
孤独总比滥情好
孤独总比滥情好 2020-12-09 07:23

I have an array like this

var words = [
    {
        word: \'Something\',
        link: \'http://www.something.com\'
    },
    {
        word: \'Something         


        
4条回答
  •  暖寄归人
    2020-12-09 07:35

    It would be possible to do it with something like:

    $('*:contains("string to find")');
    

    the problem with this approach is that "*" will return all elements that contain the string, including HTML, BODY, etc... and after that you still need to find the string inside the text node of each element, so it may be easier to just go and check every text node...

    I'd suggest you take a look at the highlight plugin that already does something very similar to what you want (instead of linking, it highlights any text on a page), but from the source code it seems pretty easy to change it.

提交回复
热议问题