简体   繁体   English

文档就绪时元素宽度未定义

[英]element width is undefined on document ready

I am trying to obtain an element width using the following code just before the </body> 我试图在</body>之前使用以下代码获取元素宽度

<script type="text/javascript" charset="utf-8">
    $(document).ready(function(){
        var diff = $('div.canvas img.photo').get(1).width;
        console.log(diff);
    });
</script> 

but it logs undefined. 但它记录未定义。 However, if I run $('div.canvas img.photo').get(1).width directly in the Chrome/Firebug console, it returns the correct width. 但是,如果我直接在Chrome / Firebug控制台中运行$('div.canvas img.photo').get(1).width ,它将返回正确的宽度。 The image is not being loaded with Javascript, so should be in place when the document ready fires. 图像没有加载Javascript,因此应该在文档准备好时触发。 What am I doing wrong? 我究竟做错了什么?

No, it shouldn't. 不,它不应该。 document.ready fires when all HTML has been loaded, but not the images. document.ready在加载所有HTML时触发,但不是图像。 If you want to wait until all images are loaded, use window.load. 如果要等到加载所有图像,请使用window.load。

Example: 例:

$(window).load(function(){
    var diff = $('div.canvas img.photo').get(1).width;
    console.log(diff);
});

Also, as some people have pointed out, you were attempting to get the property ".width" twice. 此外,正如一些人指出的那样,你试图将属性“.width”两次。

If at all possible, set a width on the imagetags in CSS. 如果可能的话,在CSS中的imagetags上设置宽度。 That way, you can safely use document.ready for your code. 这样,您就可以安全地将document.ready用于您的代码。 Using window.load will naturally delay the execution of your script, could be a second, could be ten, depending on the speed of the clients connection, and the amount of content on your page. 使用window.load自然会延迟脚本的执行,可能是一秒钟,可能是十秒,具体取决于客户端连接的速度以及页面上的内容量。 This could cause unwanted jumps and jitters if you're performing any styling. 如果您正在执行任何样式,这可能会导致不必要的跳跃和抖动。

img isn't being loaded on DOM ready. img没有在DOM上加载。 docs : docs

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. 虽然JavaScript提供了在呈现页面时执行代码的加载事件,但在完全接收到所有资产(如图像)之前,不会触发此事件。 In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. 在大多数情况下,只要完全构造DOM层次结构,就可以运行脚本。 The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. 传递给.ready()的处理程序保证在DOM准备好后执行,因此这通常是附加所有其他事件处理程序并运行其他jQuery代码的最佳位置。 When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts. 使用依赖于CSS样式属性值的脚本时,在引用脚本之前引用外部样式表或嵌入样式元素很重要。

change to this: 改为:

$(window).load(function(){
            var diff = $('div.canvas img.photo').get(1).width;
            console.log(diff.width);
        });

Image's width would only be available when its loaded completely. 图像的宽度仅在完全加载时才可用。 jQuery supports onload event on every images too. jQuery也支持每个图像上的onload事件。

You can use, 您可以使用,

$('div.canvas img.photo').load(function(){
  // here the image (or all images which match selector) has been loaded.
}

The problem is that your image is not loaded yet when you try to get its dimentions. 问题是,当您尝试获取其尺寸时,您的图像尚未加载。 To make it work wrap your code into $(window).load . 为了使它能够将代码包装到$(window).load Or another option. 或另一种选择。 If you know the sizes of the image you can provide width and height attributes, then it's going to work even inside DOMContentLoaded. 如果您知道图像的大小,您可以提供widthheight属性,那么它甚至可以在DOMContentLoaded内部工作。 Sometimes it's preferable because onload event takes longer to fire then 'DOMContentLoaded'. 有时它更可取,因为onload事件需要更长时间来触发'DOMContentLoaded'。 Otherwise you would need to use $(window).load , see @Andreas Carlbom answer. 否则你需要使用$(window).load .load,请参阅@Andreas Carlbom的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM