开发者

How to get width/height of a div set to 100% in HTML

Is there a way to get the width/height of a div that has been set to 100%?

Here is an example of what I am trying to do: http://jsfiddle.net/TUMsN/1/

When I click on the blue box, the w开发者_高级运维idth/height of the parent returns an empty string...


Use offsetHeight instead of style.height. You can't use style.height because the height of the element isn't given in an inline style attribute, like here:

<div style="height: 200px; ">Hello</div>

But if you use offsetHeight it will work no matter where the height has been set.

http://jsfiddle.net/marcosfromero/TUMsN/2/

Alternatively you can use jQuery and use the height() method:

http://jsfiddle.net/marcosfromero/TUMsN/3/


You need to use offsetHeight and offsetWidth.

I have updated the fiddle, have a look: http://jsfiddle.net/TUMsN/4/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜