2012-12-05 5 views
1

스마일 텍스트를 이미지로 바꾸기 위해 꽤 기본적인 jQuery 이모티콘 스크립트를 실행하고 있습니다. 위대한 작품이지만 오직 smily가 처음 나타나는 경우에만 효과가있는 것으로 보입니다. 그래서 만약 내가 똑같은 2 개의 스마일을 쓴다면 그것은 단지 하나의 이미지로 바뀝니다.Jquery 이모티콘 스크립트는 스마일리의 첫 번째 항목 만 찾습니다.

참조 바이올린 : 여기 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())); 
}); 
}; 

입니다 뭐죠 그래서 그들을 반복 할 수 웃는 나타나는 모든 작동이 기능을 복구하는 가장 좋은 방법은?

+0

교체는 첫 번째 발생을 포착합니다. 당신은 정규식을 사용해야합니다. – Popnoodles

답변

0

자바 스크립트가 대체

덕분에 잘 문자열 만 선두로부터 대체 알려져있다. 당신은 당신이 원하는 것을 얻기 위해 regex를 사용해야 할 것입니다.

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

정규식 옵션의 g는 패턴의 모든 발행 수와 일치하도록 지정합니다.

관련 문제