日期:2014-05-16 浏览次数:20457 次
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>模拟OnResize</title>
<style type="text/css">
.divTest{
width:30%;
height:300px;
border:1px solid #339966;
float:left;
margin-left:20px;
}
</style>
<script type="text/javascript">
function $(sId){
return document.getElementById(sId);
}
function sizeAdd(nChange){
var oDiv=$("div1");
oDiv.style.width=parseInt(oDiv.offsetWidth)+nChange+"px"
}
function handleResize(e){
var evt=e||event;
var srcEl=evt.target||evt.srcElement;
srcEl.innerHTML=Math.random();
}
/*-------------------------------------------////
*浏览器不支持onresize则进行扫描
*/
(function(){
if(document.all){
return;
}
window.onload=function(){
window.setInterval(function(){
var tag,tags=document.body.getElementsByTagName("*");
var resizeSet,oldW,oldH,newW,newH;
var _this,_event;
for(var i=0,len=tags.length;i<len;i++){
tag=tags[i];
resizeSet=tag.getAttribute("onresize");
if(resizeSet){
oldW=tag.getAttribute("oldW");
oldH=tag.getAttribute("oldH");
newW=parseInt(tag.offsetWidth);
newH=parseInt(tag.offsetHeight);
if(oldW &&oldH&&((oldW!=newW)||(oldH!=newH))){
try{
_this=tag;
_event={
srcElement:tag,
target:tag
};
resizeSet=resizeSet.replace(/(this|event)(?!=\w+)/g,"_$1");
eval(resizeSet);
}catch(e){}
}
tag.setAttribute("oldW",newW);
tag.setAttribute("oldH",newH);
}
}
},200);
};
})();
</script>
</head>
<body>
<input type="button" value="+" onclick="sizeAdd(20,20)"/>
<input type="button" value="-" onclick="sizeAdd(-20,-20)"/><br/>
<div id="div1" class="divTest" onresize="this.innerHTML=Math.random();">
</div>
<div id="div2" class="divTest" onresize="handleResize(event);">
</div>
</body>
</html>