document.body.clientHeight不工作? (JavaScript的) [英] document.body.clientHeight not working? (JavaScript)

查看:77
本文介绍了document.body.clientHeight不工作? (JavaScript的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

document.body.clientHeight没有返回正确的值(在下面的代码中返回0 onload和20 onresize)

document.body.clientHeight not returning correct values (in below code return 0 onload and 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>

任何评论..... plz help!

Any Comments..... plz help!

推荐答案

尝试使用 document.getElementById(viewheight)。innerHTML = window.innerHeight;

脚本的行为完全正确。
在开始时,身体的高度实际上为零(身高是文档的高度)。因为没有显示任何内容,所以高度结果为零。
调整打印的0跨度超过20px后,结果将保持在20px。

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.

如果您想获得窗口的最大高度或你可以使用的身体 Math.max(window.innerHeight,document.body.clientHeight)

If you want to get the maximum height of the window or the body you could use Math.max( window.innerHeight, document.body.clientHeight )

这篇关于document.body.clientHeight不工作? (JavaScript的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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