Javascript document.body.offsetHeight+;document.body.bottomMargin不等于document.documentElement.offsetHeight

Javascript document.body.offsetHeight+;document.body.bottomMargin不等于document.documentElement.offsetHeight,javascript,dom,Javascript,Dom,我试图锻炼iFrame的高度,但不明白为什么 document.body.offsetHeight + document.body.bottomMargin 不等于 document.documentElement.offsetHeight 当所有其他页边距设置为零且底部页边距的值低于16px时 一旦底部边距超过16px,上述两个值在FireFox中就相等,在Chrome中则在1px以内 奇怪的是,这个问题并不影响宽度的计算。经过大量的挖掘,我想出了这个方法来解决这个问题 function

我试图锻炼iFrame的高度,但不明白为什么

document.body.offsetHeight + document.body.bottomMargin 
不等于

document.documentElement.offsetHeight
当所有其他页边距设置为零且底部页边距的值低于16px时

一旦底部边距超过16px,上述两个值在FireFox中就相等,在Chrome中则在1px以内


奇怪的是,这个问题并不影响宽度的计算。

经过大量的挖掘,我想出了这个方法来解决这个问题

function getIFrameHeight(){
    function getComputedBodyStyle(prop) {
        return parseInt(
            document.defaultView.getComputedStyle(document.body, null),
            10
        );
    }

    return document.body.offsetHeight +
        getComputedBodyStyle('marginTop') +
        getComputedBodyStyle('marginBottom');
}
以及IE8及以下版本的扩展版本

function getIFrameHeight(){
    function getComputedBodyStyle(prop) {
        function getPixelValue(value) {
            var PIXEL = /^\d+(px)?$/i;

            if (PIXEL.test(value)) {
                return parseInt(value,base);
            }

            var 
                style = el.style.left,
                runtimeStyle = el.runtimeStyle.left;

            el.runtimeStyle.left = el.currentStyle.left;
            el.style.left = value || 0;
            value = el.style.pixelLeft;
            el.style.left = style;
            el.runtimeStyle.left = runtimeStyle;

            return value;
        }

        var 
            el = document.body,
            retVal = 0;

        if (document.defaultView && document.defaultView.getComputedStyle) {
            retVal =  document.defaultView.getComputedStyle(el, null)[prop];
        } else {//IE8 & below
            retVal =  getPixelValue(el.currentStyle[prop]);
        } 

        return parseInt(retVal,10);
    }

    return document.body.offsetHeight +
        getComputedBodyStyle('marginTop') +
        getComputedBodyStyle('marginBottom');
}

有没有一个简单的解决办法:-/我想知道你是否找到了。