iframe.document.body.scrollHeight是正确的值的两倍 [英] iframe.document.body.scrollHeight is double the correct value

查看:1611
本文介绍了iframe.document.body.scrollHeight是正确的值的两倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< iframe name =asdfid =asdfonload =change_height(this)src =asdf.jspwidth =250scrolling =noframeborder = 0>< / iframe>

        function change_height(iframe) {
            if (document.all) {
                // IE.
                ieheight = iframe.document.body.scrollHeight;
                iframe.style.height = ieheight;
            } else {
                // Firefox.
                ffheight= iframe.contentDocument.body.offsetHeight;
                iframe.style.height = ffheight+ 'px';

            }
        }

ieheight是实际高度的两倍这在IE7运行;没有在IE6上测试。如果我使用 scrollHeight offsetHeight ,它的值是相同的。

ieheight is twice the actual height when this runs in IE7; haven't tested on IE6. It's the same value if I use scrollHeight or offsetHeight.

这是Firefox的正确高度。

It's the correct height in Firefox.

在我通过除以IE值/ 2来修补这个之前,这是正确的方法是什么?

Before I patch this by just dividing the IE value /2, what's the right way to do this?

推荐答案

document.body 表示IE以Quirks模式运行时的视口。如果您的iframe中的文档在Quirks中, body scrollHeight 将等于其视口的高度,即。 iframe的默认高度。

document.body represents the viewport when IE is running in Quirks Mode. If the document inside your iframe is in Quirks, the scrollHeight of the body will be equal to the height of its viewport, ie. the default height of the iframe.

如果你真的需要在Quirks模式下获取document-height,你必须添加一个额外的wrapper div来测量。一个更好的解决方法是确保所有的文档都使用标准模式doctype。

If you really needed to get the document-height in Quirks Mode you would have to add an extra wrapper div to measure. A much better fix is to make sure all your documents use a Standards Mode doctype. You shouldn't be authoring anything with Quirks Mode in this decade.

此外,您不应使用 document.all for IE sniffing(对于支持它的其他浏览器,这可能会出错),你不应该使用 iframe.document (它是非标准的, ),你应该总是添加'px'单位(IE可以应付它,你需要在标准模式下)。

Also, you shouldn't use document.all for IE sniffing (this may go wrong for other browsers that support it), you shouldn't use iframe.document (it's non-standard and not even documented by MSDN), and you should always add 'px' units (IE can cope with it fine and you need it in Standards Mode).

function change_height(iframe) {
    var doc= 'contentDocument' in iframe? iframe.contentDocument : iframe.contentWindow.document;
    iframe.style.height= doc.body.offsetHeight+'px';
}

这篇关于iframe.document.body.scrollHeight是正确的值的两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆