如何在IE中更改src后获取新的图像大小

发布于 2024-12-06 09:14:50 字数 696 浏览 1 评论 0原文

我有一个带有旋转 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

帅的被狗咬 2024-12-13 09:14:50

在 IE 中使用 offsetHeightoffsetWidth

在 IE 中查看:http://jsbin.com/abopih/5

var imgUrl = "http://www.google.com/intl/en_com/images/srpr/logo3w.png";
 var img = document.getElementById('uploaded-image');
$('#uploaded-image').click(function() {
    imgWidth = img.clientWidth;
    imgHeight = img.clientHeight;
  $('#uploaded-image').removeAttr('width').removeAttr('height').attr('src', imgUrl);
      console.info('clientWidth: ',  img.clientWidth);
      console.info('clientHeight: ',  img.clientHeight);
      console.info('offsetWidth: ', img.offsetWidth);
      console.info('offsetWidth: ', img.offsetWidth);
});

Use offsetHeight and offsetWidth in IE.

Check this out in your IE: http://jsbin.com/abopih/5

var imgUrl = "http://www.google.com/intl/en_com/images/srpr/logo3w.png";
 var img = document.getElementById('uploaded-image');
$('#uploaded-image').click(function() {
    imgWidth = img.clientWidth;
    imgHeight = img.clientHeight;
  $('#uploaded-image').removeAttr('width').removeAttr('height').attr('src', imgUrl);
      console.info('clientWidth: ',  img.clientWidth);
      console.info('clientHeight: ',  img.clientHeight);
      console.info('offsetWidth: ', img.offsetWidth);
      console.info('offsetWidth: ', img.offsetWidth);
});
烟沫凡尘 2024-12-13 09:14:50

您可以创建一个隐藏的 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. Use visibility:hidden, grab the dimensions, and then remove it.

撩动你心 2024-12-13 09:14:50

确保您也删除了 css 高度和宽度:

$('#uploaded-image').css({ height: "auto", width: "auto" });

Make sure you remove css height and width as well:

$('#uploaded-image').css({ height: "auto", width: "auto" });
微暖i 2024-12-13 09:14:50
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="file" id="file" />
<script type="text/javascript" charset="utf-8">
$("#file").change(function(e) {
    var file, img;
    file = document.getElementById("file");
    if (file!=null) {
        img = new Image();
        img.src = file.value;
        img.onload = function() {
            alert(img.width + " " + img.height);
        };
        img.onerror = function() {
            alert( "not a valid file: " + file.type);
        };
    }

});
</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="file" id="file" />
<script type="text/javascript" charset="utf-8">
$("#file").change(function(e) {
    var file, img;
    file = document.getElementById("file");
    if (file!=null) {
        img = new Image();
        img.src = file.value;
        img.onload = function() {
            alert(img.width + " " + img.height);
        };
        img.onerror = function() {
            alert( "not a valid file: " + file.type);
        };
    }

});
</script>
谁的年少不轻狂 2024-12-13 09:14:50

看来是缓存问题。

将其添加到您的图像源网址:

imgUrl += new Date().getTime();

It seems to be a cache problem.

Add this to your image source url:

imgUrl += new Date().getTime();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文