1

私はかなり基本的な jQuery 絵文字スクリプトを実行して、スマイリー テキストを画像に変更しています。これはうまく機能しますが、スマイリーが最初に出現したときにしか機能しないようです。同じスマイリーを 2 つ書くと、1 つだけが画像に変わります。

フィドルを参照してください: http://jsfiddle.net/t6vaH/

ここにJSがあります

jQuery.fn.emoticons = function(icon_folder) {
/* emoticons is the folder where the emoticons are stored*/
var icon_folder = icon_folder || "../../../../images/forum/emoticons";
//var settings = jQuery.extend({emoticons: "emoticons"}, options);
/* keys are the emoticons
 * values are the ways of writing the emoticon
 *
 * for each emoticons should be an image with filename
 * 'face-emoticon.png'
 * so for example, if we want to add a cow emoticon
 * we add "cow" : Array("(C)") to emotes
 * and an image called 'face-cow.png' under the emoticons folder   
 */
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)")};

/* Replaces all ocurrences of emoticons in the given html with images
 */
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\" alt=\""+emotes[emoticon][i]+"\"/>","g");
        }
    }
    return html;
}
return this.each(function(){
    $(this).html(emoticons($(this).html()));
});
};

スマイリーが出現するたびに機能するようにこの機能を修復して、それらを繰り返すことができるようにする最良の方法は何ですか?

ありがとう

4

1 に答える 1

0

Javascript 置換は、部分文字列の最初の出現のみを置換することでよく知られています。必要なものを実現するには、正規表現を使用する必要があります。

var emotes = {
                "angel": Array(/\(A\)/g)
             };

正規表現オプションのgは、パターンのすべての出現に一致することを指定します。

于 2012-12-05T11:48:44.887 に答える