【发布时间】:2015-12-04 13:37:17
【问题描述】:
我正在尝试使用 jquery 制作表情符号。但我有多个表情符号的问题。我从 jsfiddle.net 创建了这个demo。
如果用户编写不同的多个表情符号,jquery 代码可以正常工作,但如果用户编写两个或多个微笑,例如 :):):):) 则 jquery 代码仅显示第一个表情符号,其他表情符号将不会显示。任何人都可以在这里帮助我吗?
JS
jQuery.fn.emoticons = function(icon_folder) {
/* emoticons is the folder where the emoticons are stored*/
var icon_folder = icon_folder || "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=\"http://www.oobenn.com/"+icon_folder+"/face-"+emoticon+".png\" class=\"emoticonimg\" alt=\""+emotes[emoticon][i]+"\"/>","g");
}
}
return html;
}
return this.each(function(){
$(this).html(emoticons($(this).html()));
});
};
$(document).ready(function(){
$(".posted").emoticons();
});
【问题讨论】:
-
为什么是您的问题?在您的示例中,一切正常,所有表情符号都出现了:):):):):)
-
@P.Frank 你看到演示页面中的红色文字了吗?问题就在那里。
-
是的,我看到红色文字下方的所有表情符号
-
@P.Frank 你能给我看截图吗?因为我那里有错误?
标签: javascript jquery google-chrome safari