document.body onresize

本文深入探讨了HTML文档中使用onresize属性与window对象绑定resize事件的方法,详细介绍了如何在body标签中声明事件处理函数,并通过window.attachEvent实现事件监听。文章还对比了使用document.body和window对象绑定事件的差异,提供了有效实现页面响应窗口尺寸变化的实用技巧。

document.body没有onresize()方法,window对象才有onresize() 方法, 但可以在<body>标签中写onresize属性,如:

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>onresize</title>
</head>

<body onresize="test2()">
<script language="javascript">
function test1(){
	alert('test1');
}

function test2(){
	alert('here');
}

//document.body.attachEvent('onresize', test1);

window.attachEvent('onresize', test1);

</script>
</body>

</html>

使用document.body绑定onresize()方法无效:

document.body.attachEvent('onresize', test1);


1、要么使用onresize属性声明:

<body onresize="test2()">

2、要么使用window对象:

window.attachEvent('onresize', test1);



<html> <head> <title>试验数据管理系统</title> </head> <script type="text/javascript"> window.onload=function(){ var WSApprovalAx = document.getElementById("PlanAx"); // /* if (!WSApprovalAx) { var sHTML = '<OBJECT id="PlanAx" classid="clsid:92eb428e-1894-4ca2-8e88-20fd0bb16001" ></OBJECT>'; $('#ctrlContainer').append(sHTML).find("#PlanAx").width("100%") .height("100%"); WSApprovalAx = document.getElementById("PlanAx"); }*/ if (!WSApprovalAx) { alert("创建报表控件失败!"); } else { setCookie('KOAL_CERT_T', 'admin', 10); var userCode = getCookie('KOAL_CERT_T'); //var userCode = 'admin'; if (userCode == null || userCode == undefined) { userCode=''; } WSApprovalAx.StartLogin(userCode, getNowFormatDate()); } document.getElementById('PlanAx').setAttribute("width",document.body.clientWidth); document.getElementById('PlanAx').setAttribute("height", document.body.clientHeight); }; window.onresize = function () { var planCtrl = document.getElementById('PlanAx'); if (planCtrl != undefined && planCtrl != null) { planCtrl.setAttribute("width", document.body.clientWidth); planCtrl.setAttribute("height", document.body.clientHeight); } }; function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds(); return currentdate; } function setCookie(c_name, value, expiredays) { var exdate = new Date(); exdate.setTime(Number(exdate) + expiredays); document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); } function getCookie(c_name) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf(c_name + "=");//获取字符串的起点 if (c_start != -1) { c_start = c_start + c_name.length + 1;//获取值的起点 c_end = document.cookie.indexOf(";", c_start);//获取结尾处 if (c_end == -1) c_end = document.cookie.length;//如果是最后一个,结尾就是cookie字符串的结尾 return decodeURI(document.cookie.substring(c_start, c_end));//截取字符串返回 } } return ""; } function OnClosed() { window.open('', '_self'); window.top.close(); } </script> <body style="margin:0px; width: 100%; height: 100%; border: 0px;overflow:hidden;"> <div id="ctrlContainer"></div> <object id="PlanAx" classid="clsid:92eb428e-1894-4ca2-8e88-20fd0bb16001" codebase="HY.Docker.18.2.dll" width="0" height="0"> </object> </body> </html>
07-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值