Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

jQuery .load() not working on my image

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('Image Loaded');
});

However the popup never show up. I can't get the .load() function to work ! Anyone had issues with this?

question from:https://stackoverflow.com/questions/3588102/jquery-load-not-working-on-my-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...