<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>未命名</title>
<script>
window.onload = function(){
//alert(document.documentElement.offsetHeight);
//這個是窗口總寬度2000如果有margin會加上margin
//IE下彈出了窗口的高度720並不是body的高度
//document.documentElement 和 document.body
//document.documentElement:返回的是html元素
//document.body:返回的是body元素
//在瀏覽器版本不兼容的情況下可以用其他一些方法做出兼容性
//如: document.documentElement.scrollTop || document.body.scrollTop
//alert(document.body.clientHeight); //IE和谷歌都彈出2000不包含margin值
document.onclick = function(){
//alert(document.documentElement.scrollTop); //IE可用 360下值爲0
//alert(document.body.scrollTop); //360下可以 IE值爲0
//alert(document.body.scrollTop || document.documentElement.scrollTop); //IE 360兼容
//offsetHeight 等於width + padding + border
//clientHeight 等於width + padding
//alert(document.documentElement.clientHeight); //IE:716 360:737
//alert(document.body.offsetHeight); //360:2000 IE8:2000;
//document.documentElement.clientHeight 只能計算窗口的高度
};
};
</script>
<style>
#div1{height:200px;width:300px;background:red;border:5px solid #333;}
</style>
</head>
<body style="height:2000px;">
<div id="div1">ds</div>
</body>
</html>
DOM獲取body寬度高度時問題
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.