document.body.clientHeight 不起作用?(JavaScript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14001483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
document.body.clientHeight not working? (JavaScript)
提问by abhishek-23
document.body.clientHeight not returning correct values (in below code return 0 onload and 20 onresize )
document.body.clientHeight 没有返回正确的值(在下面的代码中返回 0 onload 和 20 onresize )
<html>
<head>
<title>Untitled</title>
<script type="text/javascript" >
function height1()
{
document.getElementById("viewheight").innerHTML = document.body.clientHeight;
}
</script>
</head>
<body onload="height1();" onresize="height1();">
<span id="viewheight"></span>
</body>
</html>
Any Comments..... plz help!
任何评论..... 请帮忙!
回答by bukart
Try it with document.getElementById("viewheight").innerHTML = window.innerHeight;
试试看 document.getElementById("viewheight").innerHTML = window.innerHeight;
The behaviour of your script is totally correct. At the start the height of your body is really zero (body height is the height of the document). Because there's nothing shown, the height results with zero. After resizing your printed "0" spans over 20px, so the result will stay at 20px.
您的脚本的行为是完全正确的。一开始,您的身体高度实际上为零(身体高度是文档的高度)。因为没有显示任何内容,所以高度结果为零。调整打印的“0”大小后,跨度超过 20 像素,因此结果将保持在 20 像素。
If you want to get the maximum height of the window or the body you could use Math.max( window.innerHeight, document.body.clientHeight )
如果你想获得窗口或身体的最大高度,你可以使用 Math.max( window.innerHeight, document.body.clientHeight )
回答by Preet
.clientHeightgives us a Integer value only. But here scale-type of size is missing that's why it is not working correctly just add + "px"just after document.body.clientHeight.
.clientHeight只给我们一个整数值。但是这里缺少 scale-type 的大小,这就是为什么它不能正常工作的原因,+ "px"只是在document.body.clientHeight.
<script>
function height1() {
document.getElementById("viewheight").innerHTML = document.body.clientHeight + "px";
}
</script>

