Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “替换内部图像”;代码“;标签_Javascript_Jquery_Replace - Fatal编程技术网

Javascript “替换内部图像”;代码“;标签

Javascript “替换内部图像”;代码“;标签,javascript,jquery,replace,Javascript,Jquery,Replace,我正在尝试用此JSFIDLE上的纯文本替换所有笑脸图像: 如您所见,我正在尝试使用: $("code").each(function () { var a = ['<img class="smile" src="http://static.yamma.org/images/icons/smile.png">', '&lt;img class="laugh" src="http://static.yamma.org/images/icons/laugh.png"&am

我正在尝试用此JSFIDLE上的纯文本替换所有笑脸图像:

如您所见,我正在尝试使用:

$("code").each(function () {
    var a = ['<img class="smile" src="http://static.yamma.org/images/icons/smile.png">', '&lt;img class="laugh" src="http://static.yamma.org/images/icons/laugh.png"&gt;'],
        b = [":)", ":D"],
        inner = $(this).children('img').size();
    for (var d = 0; d < inner; d++) {

        var c = $(this).html();
        var e = c.replace(a[d], b[d]);
        $(this).text(e)
    }
});​
我不明白为什么要加&;lt


有解决办法吗?有没有人知道如何将某些笑脸图像替换为纯文本。

您是要替换整个图像标记还是将文本笑脸作为图像标记的文本?我不确定第二个选项是否有多大意义,但在我看来,它就像您的代码所做的一样。下面是一些代码:

下面是用新的span标记替换整个标记的代码:

但这只会替换每张图像的第一个笑脸

String.replace
就是这样工作的。为了替换所有发生的事件,您需要将全局标志(
g
)设置为启用,或者在循环中进行替换

我不明白为什么要加&;lt


由于您将以html(
var c=$(this.html();
)的形式获取,并以文本(
$(this.text)(e)
)的形式回写。

为了解决您问题的最后一部分,即另一种方法,我可以为您提供:

var emotions = {
    'smile' : ':)',
    'laugh' : ':D'
};

var img = $('code img').each(
    function(){
        var s = this.src.split('/').pop(),
            emo = s.substring(0,s.lastIndexOf('.'));
        if (emo == 'laugh' || emo == 'smile'){
            console.log(emotions[emo]);
            $(this)
                .replaceWith('<span class="emotion">' + emotions[emo] + '</span>');
        }
    });​
var={
“微笑:”:“:)”,
“笑”:“D”
};
变量img=$('code img')。每个(
函数(){
var s=this.src.split('/').pop(),
emo=s.substring(0,s.lastIndexOf('.');
如果(emo==‘笑’| | emo==‘笑’){
console.log(情绪[emo]);
$(本)
.替换为(''+情绪[emo]+'');
}
});​
以下是您的答案:

$('img').each(function(){
    switch($(this).attr('class')){
        case "smile":
            $(this).replaceWith(':)');
            break;
        case "laugh":
            $(this).replaceWith(':D');
            break;
    }   
});
这里是

$('img').each(function(){
    switch($(this).attr('class')){
        case "smile":
            $(this).replaceWith(':)');
            break;
        case "laugh":
            $(this).replaceWith(':D');
            break;
    }   
});