开发者

Detecting when HTML image source update is done

开发者 https://www.devze.com 2022-12-16 08:28 出处:网络
I\'m using jQuery开发者_开发知识库 to update an image source $(\"#myImage\").attr(\"src\", imagePath);

I'm using jQuery开发者_开发知识库 to update an image source

$("#myImage").attr("src", imagePath);

Immediately after, I try to detect the image's width with

$("#myImage").width();

Looks like sometimes (especially on first update) the update is not done, and I get invalid data.

Is there a way to make sure the image finished loading?


$.load(); will fire on the image when it's finished loading:

$("img.myImage").load(function(){
  alert("My width is " + $(this).width());
});

Source: http://api.jquery.com/load-event/


The image has a onload event like the document's body.

Make sure you set that event before you change the src.

0

精彩评论

暂无评论...
验证码 换一张
取 消