3

I have a div element, that has no defined width or height. It holds other elements, and molds on their size. I need to get the final size of the div, without being able to check the sizes of the elements inside of it (dynamic elements).

I tried parsing all the properties of the object, but found nothing, all were "undefined".

Any thoughts?

Many thanks

Later edit:

I have

<div id="xxx" style="position:relative;">  
    aaaa
</div>

I tried:

var a = mydiv.style.width;

But it did not work.

I also tried:

var getKeys = function (obj) {  
    var keys = [];

    for(var key in obj){  
        document.write(key+"="+obj.key+"<br>");  
    }  

    return keys;  
}  

getKeys(mydiv.style);  

To show all the properties, but none had any info.

The jQuery solution works perfectly, but getComputedStyle was what I was looking for, as I can't use jquery here.

Sorry for the short information.

4

3 回答 3

7

基于没有任何关于你在做什么的信息,我建议你应该(如果可能的话)使用这种window.getComputedStyle()方法,它可以找到浏览器呈现的对象的属性:

var elem = document.getElementById('elemId'),
    properties = window.getComputedStyle(elem, null),
    height = properties.height,
    width = properties.width;

如果您有可用的 jQuery,那么您可以简单地使用width()and height()(或outerWidth()/ outerHeight())方法:

var height = $('#elem').height(),
    width = $('#elem').width();
于 2012-09-20T17:59:29.600 回答
4

你没有提供很多信息。我建议您使用您尝试使用的实际代码编辑您的帖子。

但是,使用 JQuery:

$(DOMElement).width()           //-- Element width.
$(DOMElement).innerWidth()      //-- Element width + padding.
$(DOMElement).outerWidth()      //-- Element width + padding & border.
$(DOMElement).outerWidth(true)  //-- Element width + padding, border, and margin.
于 2012-09-20T18:00:12.420 回答
0

$('div').width()将返回宽度...

于 2012-09-20T18:00:19.347 回答