javascript应用:Iframe自适应其加载的内容高度

2018-10-15 17:02

阅读:649

main.htm:
复制代码 代码如下:
<html>
<head>
<metahttp-equiv=Content-Typecontent=text/html;charset=gb2312>
<metaname=authorcontent=F.R.Huang(meizz梅花雪)//
<title>iframe自适应加载的页面高度</title>
</head>

<body>
<iframesrc=child.htm></iframe>
</body>
</html>


child.htm:
<html>
<head>
<metahttp-equiv=Content-Typecontent=text/html;charset=gb2312>
<metaname=authorcontent=F.R.Huang(meizz梅花雪)//
<title>iframe自适应其加载的网页(多浏览器兼容)</title>
<scriptlanguage=javascript>
functioniframeAutoFit()
{
try
{
if(window!=parent)
{
vara=parent.document.getElementsByTagName(IFRAME);
for(vari=0;i<a.length;i++)//author:meizz
{
{
varh=document.body.scrollHeight;
if(document.all){h+=4;}
if(window.opera){h+=1;}
a[i].style.height=h;
}
}
}
}
catch(ex)
{
alert(脚本无法跨域操作!);
}
}
if(document.attachEvent)window.attachEvent(onload,iframeAutoFit);
elsewindow.addEventListener(load,iframeAutoFit,false);
</script>
</head>
<body>
<divstyle=width:200;height:400;background-color:yellow>
iframe自适应其加载的网页(多浏览器兼容)
</div>
</body>
</html>


评论


亲,登录后才可以留言!