jQuery callback on image load (even when the image is cached)

前端 未结 14 1199
轻奢々
轻奢々 2020-11-21 06:25

I want to do:

$(\"img\").bind(\'load\', function() {
  // do stuff
});

But the load event doesn\'t fire when the image is loaded from cache

14条回答
  •  醉酒成梦
    2020-11-21 07:03

    You can also use this code with support for loading error:

    $("img").on('load', function() {
      // do stuff on success
    })
    .on('error', function() {
      // do stuff on smth wrong (error 404, etc.)
    })
    .each(function() {
        if(this.complete) {
          $(this).load();
        } else if(this.error) {
          $(this).error();
        }
    });
    

提交回复
热议问题