开发者

Get the size in 'px' instead of percents

开发者 https://www.devze.com 2022-12-29 10:59 出处:网络
I have to write a javascript function that is returning the current size (in px) of a div. Unfortunately the div has its weight specified in % instead of px.

I have to write a javascript function that is returning the current size (in px) of a div. Unfortunately the div has its weight specified in % instead of px.

The style of the div: position: absolute; width: 100%; height: 100%;

And my width returning function:

function get开发者_StackOverflow社区TableWidth(tableId){
    var tabWidth = document.getElementById('pt1::tabb').children[0].children[0].style.width;
    return tabWidth;
}

tabWidth is '100%'.

Is it possible to return the px width instead of the % width ?

NOTE: I don't have access to any html/css , as the page I am working on is generated through a complex framework. I can only embed javascript.


Each browser is different. In most you can use the clientWidth and clientHeight properties of the DOM element. In non-IE browsers you can use document.defaultView.getComputedStyle(). However I'd recommend using a framework that takes care of cross-browser issues for you. In jQuery, you can get the current width in pixels using something as simple as $(element).width().


node.offsetWidth

should do the trick (in all browsers!)

0

精彩评论

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