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

前端 未结 14 1526
轻奢々
轻奢々 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:20

    Just re-add the src argument on a separate line after the img oject is defined. This will trick IE into triggering the lad-event. It is ugly, but it is the simplest workaround I've found so far.

    jQuery('', {
        src: url,
        id: 'whatever'
    })
    .load(function() {
    })
    .appendTo('#someelement');
    $('#whatever').attr('src', url); // trigger .load on IE
    

提交回复
热议问题