首页 > 语言 > JavaScript > 正文

用document.documentElement取代document.body的原因分析

2024-05-06 14:12:51
字体:
来源:转载
供稿:网友
IE6在页面内容超出窗口大小时将宽度属性scrollWidth、clientWidth、offsetWidth都解释为内容实际宽度。
上次的测试说明了document.body属性并不会给我们返回预期的结果,比如我们用document.body.clientHeight原本想取得“页面可见区域高度”,可实际上返回的是“页面实际内容高度”。
那我们怎么办呢?难道加上了文档DTD类型之后就再也不能取到“可见区域高度”和“内容实际宽度”等等属性了吗?
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>documentElement</title>
<style type="text/css">
body{margin:0;padding:0;font:12px/150% arial;}
</style>
<script type="text/javascript">
function a() {
var scrollTop;
var scrollLeft;
if (typeof window.pageYOffset != 'undefined') {
scrollTop = window.pageYOffset;
scrollLeft = window.pageXOffset;
}
else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
scrollTop = document.documentElement.scrollTop;
scrollLeft = document.documentElement.scrollLeft;
}
else if (typeof document.body != 'undefined') {
scrollTop = document.body.scrollTop;
scrollLeft = document.body.scrollLeft;
}
var scrollHeight = document.documentElement.scrollHeight;
var scrollWidth = document.documentElement.scrollWidth;
var clientWidth = document.documentElement.clientWidth;
var clientHeight = document.documentElement.clientHeight;
var offsetWidth = document.documentElement.offsetWidth;
var offsetHeight = document.documentElement.offsetHeight;
var screenTop = window.screenTop;
var screenBottom = window.screenBottom;
var screenLeft = window.screenLeft;
var sheight = window.screen.height;
var swidth = window.screen.width;
var availHeight = window.screen.availHeight;
var availWidth = window.screen.availWidth;
document.getElementById('scrollTop').value = scrollTop;
document.getElementById('scrollLeft').value = scrollLeft;
document.getElementById('scrollHeight').value = scrollHeight;
document.getElementById('scrollWidth').value = scrollWidth;
document.getElementById('clientWidth').value = clientWidth;
document.getElementById('clientHeight').value = clientHeight;
document.getElementById('offsetWidth').value = offsetWidth;
document.getElementById('offsetHeight').value = offsetHeight;
document.getElementById('screenTop').value = screenTop;
document.getElementById('screenBottom').value = screenBottom;
document.getElementById('screenLeft').value = screenLeft;
document.getElementById('sheight').value = sheight;
document.getElementById('swidth').value = swidth;
document.getElementById('availHeight').value = availHeight;
document.getElementById('availWidth').value = availWidth;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选