Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.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 图像的Src设置不正确_Javascript_Jquery - Fatal编程技术网

Javascript 图像的Src设置不正确

Javascript 图像的Src设置不正确,javascript,jquery,Javascript,Jquery,我有一些div的类名为class='tweetCon' 每个包含1个图像,我想检查图像源是否存在,因此如果可用,我不做任何事情,但如果不可用,我将使用适当的图像更改图像源我的代码如下: $('.tweetimgcon').children('img').each(function () { imageExists($(this).attr("src"), function (exists) { if (exists == true) { } else {

我有一些div的类名为class='tweetCon'
每个包含1个图像,我想检查图像源是否存在,因此如果可用,我不做任何事情,但如果不可用,我将使用适当的图像更改图像源我的代码如下:

$('.tweetimgcon').children('img').each(function () {
    imageExists($(this).attr("src"), function (exists) {
        if (exists == true) {
        } else {
            $(this).attr("src", "https://pbs.twimg.com/profile_images/2284174758/v65oai7fxn47qv9nectx.png");
        }
    }, function () {
    });
});
function imageExists(url, callback,callback2) {
    var img = new Image();
    img.onload = function() { callback(true);callback2(); };
    img.onerror = function() { callback(false);callback2(); };
    img.src = url;
}
imageExists()也如下所示:

$('.tweetimgcon').children('img').each(function () {
    imageExists($(this).attr("src"), function (exists) {
        if (exists == true) {
        } else {
            $(this).attr("src", "https://pbs.twimg.com/profile_images/2284174758/v65oai7fxn47qv9nectx.png");
        }
    }, function () {
    });
});
function imageExists(url, callback,callback2) {
    var img = new Image();
    img.onload = function() { callback(true);callback2(); };
    img.onerror = function() { callback(false);callback2(); };
    img.src = url;
}
现在的问题是,不可用图像的src设置不正确,尽管当我通过console.log检查这些图像的src时,它显示它们已正确设置,但未显示,当我使用chrome的inspect元素时,我可以看到src未设置。有人能帮我吗?

要点是
$(this)
不指向您的对象,因为您在
imgExists
回调函数中调用
$(this)
,但不是jQuery回调函数
每个
,因此
this
对象不指向您原始的
img
标记

解决方案应该是先保存对象引用,然后尝试以下操作:

$('.tweetimgcon').children('img').each(function () {
    var _this = $(this);
    imageExists($(this).attr("src"), function (exists) {
        if (exists == true) {
        } else {
            _this.attr("src", "https://pbs.twimg.com/profile_images/2284174758/v65oai7fxn47qv9nectx.png");
        }
    }, function () {
    });
});

为了获得更好的回调函数,请使用call

function imageExists(url, cb, ecb) {
var img = new Image();
    img.onload = function() { cb.call(img,true,url); };
    img.onerror = function() { ecb.call(img,false,url); };
    img.src = url;
}
我添加了
img
作为第一个参数,然后这将是您的
this
关键字,而不是窗口


只需重新初始化
var$this=$(this).attr(“src”)
并执行
$(this).attr(“src”),…
最好使用
src
属性而不是属性。
$(this).prop(“src”)
谢谢@karthikr我将其更改为this,但仍然不起作用:var th=$(this.attr(“src”);$(th).attr(“src”),)谢谢你我照你说的做了,但还是一样的问题:(@Hamed Minaee谢谢你