使用jquery将文本转换为表情符号

时间:2014-02-08 07:29:07

标签: jquery

我使用下面的代码将文本转换为表情符号。它将一类“内容”的文本转换为表情符号。它工作正常,但内容也通过ajax更新。那些“内容”div不会获得转换后的表情符号。如何做到对不对?

jQuery.fn.emoticons = function(icon_folder) {
          var icon_folder = icon_folder || "/site-media/static/images/emoticons";
          var emotes = {"smile": Array(":-)",":)","=]","=)"),
              "sad": Array(":-(","=(",":[",":<"),
              "wink": Array(";-)",";)",";]","*)"),
              "grin": Array(":D","=D","XD","BD","8D","xD"),
              "surprise": Array(":O","=O",":-O","=-O"),
              "devilish": Array("(6)"),
              "angel": Array("(A)"),
              "crying": Array(":'(",":'-("),
              "plain": Array(":|"),
          "smile-big": Array(":o)"),
          "glasses": Array("8)","8-)"),
          "kiss": Array("(K)",":-*"),
          "monkey": Array("(M)")};

      function emoticons(html){
        for(var emoticon in emotes){
          for(var i = 0; i < emotes[emoticon].length; i++){
        /* css class of images is emoticonimg for styling them*/
          html = html.replace(emotes[emoticon][i],"<img src=\""+icon_folder+"/face-"+emoticon+".png\" class=\"emoticonimg\" height=\"20\" width=\"20\" alt=\""+emotes[emoticon][i]+"\"/>","g");
          }
        }
        return html;
      }
      return this.each(function(){
        $(this).html(emoticons($(this).html()));
      });
    };

    $('.content').emoticons();

0 个答案:

没有答案