<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="application/javascript" src="js/jquery.min.js"></script>
<script type="application/javascript">
$(function() {
var initHeight = document.body.clientHeight;
// Firefox和Chrome早期版本中带有前缀
var MutationObserver = window.MutationObserver
|| window.WebKitMutationObserver
|| window.MozMutationObserver
// 选择目标节点
var target = document.body;
// 创建观察者对象
var observer = new MutationObserver(function(mutations) {
if(initHeight != document.body.clientHeight) {
initHeight = document.body.clientHeight;
console.log(initHeight);
}
});
// 配置观察选项:
var config = {
attributes: true
}
// 传入目标节点和观察选项
observer.observe(target, config);
// 随后,你还可以停止观察
//observer.disconnect();
});
function reHeigth() {
var height = $('input').val();
$('body').css('height', height + 'px');
}
</script>
</head>
<body>
<input type="text" />
<button onclick="reHeigth();">修改body高度</button>
</body>
</html>