获取刷新时图像的高度
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
document.ready
在 DOM 加载后触发,但不一定在图像和 CSS 加载后触发。如果您在window.onload
上运行该代码,您应该在各个浏览器中获得一致的结果。尝试使用 jQuery 的
load
处理程序来代替:document.ready
fires after the DOM has loaded, but not necessarily after the images and CSS have loaded. If you run that code onwindow.onload
, you should get consistent results across the browsers.Try using jQuery's
load
handler instead:尝试等待图像完全加载
Try waiting until the image is completely loaded