javascript - 图像加载事件,在出现错误时自动重新加载图像

标签 javascript jquery load

如何构建<img>发生错误时自动重新加载图像的加载事件?

图像的创建方式:

 var Img = $(document.createElement('img'));
 Img.on('load',function(){
  // Some code, this handler must be saved
 });
 Img.attr('src',path);
 Img.appendTo(...

使用地点:http://vseslava.ru ,索引上的大圆圈。

最佳答案

您可以从此代码中使用:

$('#image1')
    .load(function(){
        $('#result1').text('Image is loaded!'); 
    })
    .error(function(){
        $('#result1').text('Image is not loaded!');
            // in this case you must reload image with jQuery
    });

这很简单,因为你有图像网址,你可以重新加载它并将其替换为图像。

更新

$("img").load(function(){
        $('#result1').text('Image is loaded!\n');
    })
    .error(function(){
        $('#result1').text('Image is not loaded!\n');
        // in this case you must reload image with jQuery
        var imgSrc = $(this).attr('src');
        $(this).attr('src',imgSrc);
});

Try Demo

关于javascript - 图像加载事件,在出现错误时自动重新加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13331284/

相关文章:

javascript - 在响应式图片上显示/隐藏链接

具有多行数据的 MySQL LOAD DATA

javascript - 哪个谷歌地图 API 用于在区域选择上呈现此鼠标

javascript - Meteor 的滚动事件

wpf - 如何在 WPF 图像中加载非常大的源图像?

javascript - jQuery each() 和 load() 排序

Javascript——如果 h2_JSON 是一个对象 {}(如我的 console.log 所示——为什么它会落入 else 循环?

javascript - 如何在点击功能上调用javascript方法?

javascript - 在获取 javascript 映射之前使用 _c

javascript - jQuery 隐藏显示幻灯片悬停