开发者

Get the size in 'px' instead of percents

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜