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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-17 我要评论
想了解Iframe自适应其加载的内容高度的相关内容吗,在本文为您仔细讲解的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Iframe自适应其加载的内容高度,下面大家一起来学习吧。
<html>  
    <head>  
       <meta  http-equiv='Content-Type'  content='text/html;  charset=gb2312'>  
       <meta  name='author'  content='站长中国 http://www.zzcn.net/'>  
       <title>iframe自适应加载的页面高度</title>  
    </head>  

    <body>
        <iframe src="child.htm"></iframe>
    </body>
</html>

child.htm:

<html>  
<head>  
   <meta  http-equiv='Content-Type'  content='text/html;  charset=gb2312'>  
   <meta  name='author'  content='站长中国 http://www.zzcn.net/'>  
   <title>iframe  自适应其加载的网页(多浏览器兼容)</title>  
   <script  language=javascript>
   function iframeAutoFit()
   {
      try
      {
         if(window!=parent)
         {
          var a = parent.document.getElementsByTagName("IFRAME");
            for(var i=0; i<a.length; i++) //author:meizz
            {
               if(a[i].contentWindow==window)
               {
                   var h = 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);  
   else  window.addEventListener('load',  iframeAutoFit,  false);  
   </script>  
</head>  
<body>  
   <div  style="width:  200;  height:  400;  background-color:  yellow">  
       iframe  自适应其加载的网页(多浏览器兼容)  
   </div>  
</body>  
</html>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们