Why would jquery return 0 for an offsetHeight when firebug says it's 34?

后端 未结 5 1212
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 04:43

So I have a div whose content is generated at runtime it initially has no height associated with it. When it\'s generated according to firebug and from what I can alert wit

5条回答
  •  太阳男子
    2020-12-19 05:31

    Are you sure the element is included in the document tree, and rendered? (ie. not “display: none”, but “visibility: hidden” is OK.)

    An element that is not actually taking part in the document render process has no dimensions, and will give an offsetWidth/Height of 0.

    ETA after code sample added: with your exact code, I get offsetHeight on the div of ‘17’. The first image is sized correctly, the second has no size.

    This is correct as rendered(*). Any images that fail to load are replaced by their alt text as an inline span. Your image has no alt text, so it is replaced by an empty string. Normally, as an inline element, this cannot be set to any particular size. The exception is the first image, because you've given it ‘display: block’, which makes it amenable to the explicit sizing provided by width/height.

    In Quirks Mode, you would have got little ‘broken image’ icons sized the same as the images were supposed to be. This does not happen in Standards Mode because it is assumed that you know how to use alt text properly if you're using standards.

    Either way, the dimensions measurement works fine for me if I replace the "..." URLs with real working addresses.

    (*: although you can't actually see it because of the rather weird ‘overflow-hidden’ on html and ‘left: 1597px;’ combo. Well, unless you have a really wide monitor!)

提交回复
热议问题