jQuery的:图像宽度在domready或加载? [英] jquery: image width on domready or load?

查看:73
本文介绍了jQuery的:图像宽度在domready或加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery data()方法存储图像的宽度.

hey guys, I'm using the jquery data() method to store the width of images.

jquery何时能够获取图像的宽度.在准备就绪还是正在加载? 是否需要加载图像以查询其宽度,或者我也可以在domready上查询宽度吗?

When is jquery able to get the width of images. on-dom-ready or on-load? Is it necessary to have the images loaded to query it's width, or can i query the width on domready as well?

谢谢

这是我的代码:

    //Save the original image width
    $(".post-body img").each(function() {  
        $(this).data('width', $(this).width());
    });

推荐答案

这取决于如果您指定宽度,那么DOM准备就绪就可以了.如果您依赖于图像本身的宽度,则需要使用window.onload检查图像加载后的宽度,例如,在加载图像后会触发,例如:

It depends, if you're specifying the width, then DOM ready works fine. If you're depending on the width coming from the image itself, you need to check once it's loaded, using window.onload which fires after images are loaded, for example:

$(window).load(function() {
  var w = $("img").width();
});

这篇关于jQuery的:图像宽度在domready或加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆