【发布时间】:2010-03-23 02:59:40
【问题描述】:
我正在尝试获取 html 窗口内容的高度。这是内容的完整高度,而不是可见高度。我使用以下方法取得了一些(非常有限的)成功:
document.getElementsByTagName('html')[0].offsetHeight 在 FireFox 中。
然而,这在 IE 中失败,在 Chrome 中使用绝对定位元素时失败 (http://code.google.com/p/chromium/issues/detail?id=38999)。
可用于重现此内容的示例 html 文件是:
<html>
<head>
<style>
div {
border:solid 1px red;
height:2000px;
width:400px;
}
.broken {
position:absolute;
top:0;
left:0;
}
.fixed {
position:relative;
top:0;
left:0;
}
</style>
<script language='javascript'>
window.onload = function () {
document.getElementById('window.height').innerHTML = window.innerHeight;
document.getElementById('window.screen.height').innerHTML = window.screen.height;
document.getElementById('document.html.height').innerHTML = document.getElementsByTagName('html')[0].offsetHeight;
}
</script>
</head>
<body>
<div class='fixed'>
window.height: <span id='window.height'> </span> <br/>
window.screen.height: <span id='window.screen.height'></span> <br/>
document.html.height: <span id='document.html.height'></span> <br/>
</div>
</body>
</html>
谢谢大家
吉多·塔皮亚
【问题讨论】:
-
我认为 document.documentElement.scrollHeight 解决了我的问题。仍在所有浏览器上进行测试,但到目前为止看起来相当可靠
-
根据 quirksmode,IE 的 scrollHeight 很不稳定。但这可能很微妙,可以忽略:quirksmode.org/dom/w3c_cssom.html#elementview
-
嗨 Anthony,我正在做测试,它似乎适合我的目的。我已经测试过 Opera、Safari、Chrome、FF 和 IE8,它似乎可以满足我的要求。
标签: javascript html css