获取刷新时图像的高度

发布于 2024-10-07 01:03:39 字数 321 浏览 7 评论 0原文

我正在尝试使用 jquery 非常基本地获取图像的高度

$(document).ready(function() {
    alert($('#image').height());
})

。不过我很困惑。
如果我按 F5,我会得到以下结果:
火狐浏览器:383px
IE 8:30 像素
Chrome:0px

如果我通过链接转到该页面:
火狐浏览器:383px
IE 8:383px
Chrome: 383px

383 显然是正确的值。但为什么我在刷新时得到错误的值?

I'm trying to get the height of an image with jquery

$(document).ready(function() {
    alert($('#image').height());
})

Very basic. However I'm confused.
If I press F5 I get the following result:
Firefox: 383px
IE 8: 30px
Chrome: 0px

If I go to the page via a link:
Firefox: 383px
IE 8: 383px
Chrome: 383px

383 is obviously the correct value. But why do I get the wrong value upon refresh?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

另类 2024-10-14 01:03:39

document.ready 在 DOM 加载后触发,但不一定在图像和 CSS 加载后触发。如果您在 window.onload 上运行该代码,您应该在各个浏览器中获得一致的结果。

尝试使用 jQuery 的 load 处理程序来代替:

$(window).load(function() {
    alert($('#image').height());
})

document.ready fires after the DOM has loaded, but not necessarily after the images and CSS have loaded. If you run that code on window.onload, you should get consistent results across the browsers.

Try using jQuery's load handler instead:

$(window).load(function() {
    alert($('#image').height());
})
向日葵 2024-10-14 01:03:39

尝试等待图像完全加载

    var img = document.getElementById("image");
    img.onload = function () {
    alert($('#image').height());
    };

Try waiting until the image is completely loaded

    var img = document.getElementById("image");
    img.onload = function () {
    alert($('#image').height());
    };
秋凉 2024-10-14 01:03:39
$(document).ready(function() {

    $('#image').bind("load", function() {
       alert($('#image').height());
    });

})
$(document).ready(function() {

    $('#image').bind("load", function() {
       alert($('#image').height());
    });

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