两种用法得到的数值是不一样的:
document.documentElement.offsetHeight;
document.body.offsetHeight;
为什么呢?
document.documentElement指的是html标签
document.body指的是body标签
你的两个高宽不一样,是你的margin与panding没有清零;
你可以尝试一下。
alert(document.body.offsetWidth); alert(document.documentElement.offsetWidth);
*{
margin:0;
padding: 0;
}清零之后还是不一样,好奇怪。。。。求解
294px;0px;
294px;18px;
<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
*{padding:0 ;margin:0;}
</style>
</head>
<body>
<script type="text/javascript">
document.write(document.documentElement.offsetWidth+"px;"+document.documentElement.offsetHeight+"px;<br/>")
document.write(document.body.offsetWidth+"px;"+document.body.offsetHeight+"px;")
</script>
</body>
</html>上面就是完整的代码,就是把margin,padding清零后,这两个数值是一样的。