开发者

Get elements width and height

开发者 https://www.devze.com 2023-04-10 21:10 出处:网络
How can i get height and width of element which doesn\'t a part of DOM yet? For example: var t = $(\'<div style=\"position:absolute;height:100px;wid开发者_开发百科th:100px;background-color:steelb

How can i get height and width of element which doesn't a part of DOM yet?

For example:

var t = $('<div style="position:absolute;height:100px;wid开发者_开发百科th:100px;background-color:steelblue;"  >lalala</div>');

var height = t.height(); // height is 0!


Works for me as well. You could try

var height = t.innerHeight();

though as demonstrated in this fiddle.

If that doesn't work for you (well possible depending on the browser), consider temporarily adding the element to the DOM:

var t = $('<div style="position:absolute;height:100px;width:100px;background- color:steelblue;"  >lalala</div>').hide().appendTo('body');
var height = t.height();
t = t.detach().show();
0

精彩评论

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

关注公众号