ziniulian2
6/16/2016 - 7:11 AM

测试 documentElement 和 body 的区别

测试 documentElement 和 body 的区别


function init() {
	// 测试 documentElement 和 body 的区别
	/*
		测试时间:2016-6-16
		测试环境:
			IE : 11.0.9600.18124IS
			chrome : 51.0.2704.84 m
			火狐 : 40.0.3
	*/
	document.body.style.height = "1500px";
	document.body.onclick = function() {
		document.body.scrollTop = 10;
		document.documentElement.scrollTop = 20;
		console.log ("////");
		console.log (document.body.scrollTop);
		console.log (document.documentElement.scrollTop);
	}
	var a = document.getElementsByTagName("div")[0];
	var d = a.ownerDocument.documentElement;
	var b = a.ownerDocument.body;

	console.log ("body.clientLeft = " + b.clientLeft);
	console.log ("body.clientTop = " + b.clientTop);
	console.log ("body.scrollLeft = " + b.scrollLeft);
	console.log ("body.scrollTop = " + b.scrollTop);

	console.log ("html.clientLeft = " + d.clientLeft);
	console.log ("html.clientTop = " + d.clientTop);
	console.log ("html.scrollLeft = " + d.scrollLeft);
	console.log ("html.scrollTop = " + d.scrollTop);
	/* 结论:(2016-6-16 : IE11、chrome51.0.2704.84 m、火狐40.0.3)
		在未声明 DTD 时,
			火狐、chrome :只能使用 body 获取 scrollTop 值。
			IE :body、documentElement 都能获取 scrollTop 值。
		声明 DTD 时,
			火狐、IE :只能使用 documentElement 获取 scrollTop 值。
			chrome :只能使用 body 获取 scrollTop 值。
	*/
}