Image onload for static images

I know that for the image onload to work you have to set the src after the onload handler was attached. However I want to attach onload handlers to images that are static in my HTML. Right now I do that in the following way (using jQquery):

$('#img1').load( function() { alert('foo'); }) .attr('src', $('img1').attr('src')); But this is rather ugly and has the obvious flow that it can only be done for selectors that match only one image. Is there any other, nicer way to do this? edit What I meant by that it can only be done for selector that match only one image is that when doing this:

$('.img1').load( function() { alert('foo'); }) .attr('src', $('.img1').attr('src')); That both images will have src='picture.jpg'

以上就是Image onload for static images的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准