有些文件我不能得到文件的高度(位置绝对在最底部)。此外,填充底部似乎在这些页面上不起任何作用,但在高度将返回的页面上起作用。相关案例:

http://fandango.com http://paperbackswap.com

在胡闹 jQuery的$(文档).height ();返回正确的值 文档。Height返回0 document.body.scrollHeight返回0

关于平装书互换: jQuery的$(文档).height ();TypeError: $(document)为空 文档。Height返回不正确的值 scrollheight返回错误的值

注意:我有浏览器级别的权限,如果有一些技巧在那里。


当前回答

你甚至可以用这个:

var B = document.body,
    H = document.documentElement,
    height

if (typeof document.height !== 'undefined') {
    height = document.height // For webkit browsers
} else {
    height = Math.max( B.scrollHeight, B.offsetHeight,H.clientHeight, H.scrollHeight, H.offsetHeight );
}

或者以一种更jQuery的方式(因为你说过jQuery不会撒谎):)

Math.max($(document).height(), $(window).height())

其他回答

这是一个非常老的问题,因此,有许多过时的答案。截至2020年,所有主要浏览器都遵守了该标准。

2020年的答案:

document.body.scrollHeight

编辑:上面没有把<body>标签的边距考虑在内。如果你的身体有边缘,使用:

document.documentElement.scrollHeight

我不知道如何确定高度,但你可以用这个在底部放一些东西:

<html>
<head>
<title>CSS bottom test</title>
<style>
.bottom {
  position: absolute;
  bottom: 1em;
  left: 1em;
}
</style>
</head>

<body>

<p>regular body stuff.</p>

<div class='bottom'>on the bottom</div>

</body>
</html>

2017年的正确答案是:

.height document.documentElement.getBoundingClientRect ()

与document.body.scrollHeight不同的是,该方法只考虑正文边距。 它还给出了分数高度值,这在某些情况下是有用的

我撒谎了,jQuery为两个页面返回正确的值$(document).height();…我为什么要怀疑它呢?

全文高度计算:

为了更通用,找到任何文档的高度,你可以通过简单的递归找到当前页面上最高的DOM节点:

;(function() {
    var pageHeight = 0;

    function findHighestNode(nodesList) {
        for (var i = nodesList.length - 1; i >= 0; i--) {
            if (nodesList[i].scrollHeight && nodesList[i].clientHeight) {
                var elHeight = Math.max(nodesList[i].scrollHeight, nodesList[i].clientHeight);
                pageHeight = Math.max(elHeight, pageHeight);
            }
            if (nodesList[i].childNodes.length) findHighestNode(nodesList[i].childNodes);
        }
    }

    findHighestNode(document.documentElement.childNodes);

    // The entire page height is found
    console.log('Page height is', pageHeight);
})();

你可以把这个脚本粘贴到DevTools控制台,在你的示例站点(http://fandango.com/或http://paperbackswap.com/)上测试它。

注意:它与Iframes一起工作。

享受吧!