开发者

Changing the source of an image (img) tag doesn't resize on IE

I have an img element in my DOM. Based upon user action, I compute a URL for an image, and change the image element's src attribute (using jQuery). The new image has a different size than the old image. This works fine on Safari, but IE does not resize the display for the new image's size. I do have a .load handler on the src change (in which I make some changes to the img element's class)... if I knew how to access the image's actual size, I could set the img elements height and width properties. Are开发者_JS百科 those values accessible somewhere somehow? If not, is there some other way to have the image displayed at its new size?


I'm working in Chrome, not IE, but I was having the same problem and thought I would share for anyone else that stumbles upon this question. I solved this by removing the "style" attribute from the image before changing the source, i.e.:

myImg.removeAttribute('style').setAttribute('src', newSrc);


I had the same problem, but this took care of it.

$("#img").removeAttr("height").removeAttr("width").attr("src", newsrc);


I would investigate other solutions, but you can indeed access that information directly

var myImage = new Image();
myImage.src = $("image selector goes here").attr('src');
myImage.onload = function() {
  alert(myImage.width + "," + myImage.height);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜