我使用下面的代码在javascript中将文本转换为表情符号,
function add_smile($text) {
var $text=$text;
function RegExpEscape(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
var emoticons = {
':)' : 'images/smileys/angry.gif',
':(' : 'images/smileys/sad.gif',
';)' : 'images/smileys/wink.gif'
}
var result =$text;
var emotcode;
var regex;
for (emotcode in emoticons)
{
regex = new RegExp(RegExpEscape(emotcode), 'gi');
result = result.replace(regex, function(match) {
var pic = emoticons[match.toLowerCase()];
if (pic != undefined) {
return '<img src="' + pic + '"/>';
} else {
return match;
}
});
}
$('#editor-textarea').val($('#editor-textarea').val() + " " + result);
}
在我看来,
<textarea name="content" class="editor-textarea" id="editor-textarea">
</textarea>
<?php foreach($smiles as $k=>$v) : ?>
<button type="button" class="nobutton" onclick="add_smile('<?php echo $k ?>')"><?php echo $v ?></button>
<?php endforeach; ?>
javascript函数现在用<img src="images/smileys/angry.gif">
替换文本,因为文本不显示为图像。如何解决此问题?