Replacing text with smiley image using Jquery

后端 未结 2 1664
-上瘾入骨i
-上瘾入骨i 2021-01-22 08:48

Please Guys, I am new to Jquery. I have been searching for a suitable help since last week but I can\'t find any. I want to replace a text with a smiley image using jquery but I

相关标签:
2条回答
  • 2021-01-22 09:21

    You can use following code:

    var a = [[':happy;','<img src="happy_face.gif" />'],[':sick;',"<img src='sick.gif' />"]];
    a.forEach(function(item) {
        $('p').each(function() {
            var text = $(this).html();
            $(this).html(text.replace(item[0], item[1]));
        });
    });
    

    Working example here: Smiley codes replace

    0 讨论(0)
  • 2021-01-22 09:28

    It is actually quite simple. You only need one line to find and replace string instances in the HTML: $("body").html($("body").html().replace(/:happy;/g,'<div class="happy-face"></div>'));

    This sets $("body").html() to the $("body").html() with :happy; replaced with <div class="happy-face"></div>.

    /:happy;/g is RegEx to find the string :happy; globally (g)

    https://jsfiddle.net/heowqt1u/2/

    0 讨论(0)
提交回复
热议问题