jQuery .load() not working on my image

前端 未结 2 664
半阙折子戏
半阙折子戏 2020-12-01 00:46

I have some code I am trying to run once my image has finished loading. I use this following jQuery code:

$(\"#myimageid\").load(function() {
    alert(\'Im         


        
2条回答
  •  没有蜡笔的小新
    2020-12-01 01:23

    If you're running this after the image already has a set source, you need to do an additional check for caches images (who fired the event, just before you added an event handler listening for it). You can do that like this:

    $("#myimageid").on('load', function() {
      alert('Image Loaded'); 
    }).each(function() {
      if(this.complete) $(this).load();
    });
    

    Update for later versions of query, use:

    if(this.complete) $(this).trigger('load');
    

    Using (this).load(); will produce a Cannot read property 'indexOf' of undefined error

提交回复
热议问题