我正在尝试用另一个字符串替换字符串,但这对我不起作用。我简要说明我试图用笑脸图像替换笑脸字符串,但这不起作用。请看一下代码,让我知道它有什么问题:
var comment ="Hello all how are you :)";
var emo = {'smile': ':)', 'tongue': ':P'};
$.each(emo,function(index,value) {
if(comment.contains(value)){
var emopiclink = 'http://www.abcdedif.com/emoticon/'+index+'.png';
var emopic = '<img src="'+emopiclink+'" hieght="20px" width="20px">';
comment.replace(value, emopic);
console.log(comment);
// alert("String Found");
}
});
答案 0 :(得分:0)
您需要接受来自replace()
方法的返回值,并且contains
没有使用indexOf()
var comment = "Hello all how are you :)";
var emo = {
'smile': ':)',
'tongue': ':P'
};
$.each(emo, function(index, value) {
if (comment.indexOf(value) > -1) {
var emopiclink = 'http://www.abcdedif.com/emoticon/' + index + '.png';
var emopic = '<img src="' + emopiclink + '" hieght="20px" width="20px">';
comment = comment.replace(value, emopic);
console.log(comment);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>