如何在IE中更改src后获取新的图像大小
我有一个带有旋转 gif 的图像元素。后来我用 jQuery 动态更改该图像的 src,并且我想获取新图像的实际宽度和高度。这是我的代码:
function loadImage() {
$('#uploaded-image').load(function() {
var img = document.getElementById('uploaded-image');
// These statements return the correct values in FF and Chrome but not IE
imgWidth = img.clientWidth;
imgHeight = img.clientHeight;
});
$('#uploaded-image').removeAttr('width').removeAttr('height').attr('src', imgUrl);
}
这在 Chrome 和 Firefox 中完美运行,但 IE 总是返回原始旋转器 gif 的大小而不是新图像的大小。
如何在 IE 中获取新图像的宽度和高度?
注释:
除了纯 Javascript 方法之外,我还尝试了 jQuery .width() 和 .height() 方法,得到了相同的结果。
.load 事件在所有浏览器中都按预期触发。
I have an image element with a spinner gif. I later dynamically change the src of that image with jQuery, and I want to get the actual width and height of the new image. Here is my code:
function loadImage() {
$('#uploaded-image').load(function() {
var img = document.getElementById('uploaded-image');
// These statements return the correct values in FF and Chrome but not IE
imgWidth = img.clientWidth;
imgHeight = img.clientHeight;
});
$('#uploaded-image').removeAttr('width').removeAttr('height').attr('src', imgUrl);
}
This works perfectly in Chrome and Firefox, but IE always returns the size of the original spinner gif instead of the new image.
How can I get the width and height of the new image in IE?
Notes:
I've tried the jQuery .width() and .height() methods in addition to the pure Javascript approach, with the same results.
The .load event is being fired as expected in all browsers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 IE 中使用
offsetHeight
和offsetWidth
。在 IE 中查看:http://jsbin.com/abopih/5
Use
offsetHeight
andoffsetWidth
in IE.Check this out in your IE: http://jsbin.com/abopih/5
您可以创建一个隐藏的 div 并将图像放置在那里以获得尺寸。只需确保隐藏的 div 未使用
display:none
完成,因为这样它将没有尺寸。使用visibility:hidden
,获取尺寸,然后将其删除。You could create a hidden div and place the image in there in order to get the size. Just make sure the hidden div isn't done with
display:none
, because then it will have no dimensions. Usevisibility:hidden
, grab the dimensions, and then remove it.确保您也删除了 css 高度和宽度:
Make sure you remove css height and width as well:
看来是缓存问题。
将其添加到您的图像源网址:
It seems to be a cache problem.
Add this to your image source url: