你好,欢迎访问远方教程PC版!
广告位招租

iframe自适应高度

[日期:2013-10-16]   来源:远方教程  作者:远方教程   阅读:2008次[字体: ] 访问[旧版]
 捐赠远方教程 

  iframe是非常常用的网页代码,这一讲给大家讲如何让iframe去自适应高度。

function SetWinHeight(obj){
 var win=obj;
 if (document.getElementById){
  if (win && !window.opera){
   if (win.contentDocument && win.contentDocument.body.offsetHeight)
    win.height = win.contentDocument.body.offsetHeight;
   else if(win.Document && win.Document.body.scrollHeight)
    win.height = win.Document.body.scrollHeight;
 }
}

 <iframe src="center.jsp" allowtransparency="true"  name="right" width="100%" height="500" scrolling="no"
         frameborder="0" id="window3" border="0" noresize="noresize" framespacing="0" onload="Javascript:SetWinHeight(this)"></iframe>   

//---------------------------
//多层iframe嵌套时,里层的iframe自适应高度的同时,控制外层iframe的高度(应该可以达到自适应,本例将外层iframe的高度写死)
//注意,获取外层iframe的方法parent.document.getElementById().height='450px';
//注意,附值高度时,加“px”像素标记。
<script type="text/javascript">

  function SetWinHeight(obj){
   var win=obj;
   if (document.getElementById){
    if (win && !window.opera){
     if (win.contentDocument && win.contentDocument.body.offsetHeight) {
      win.height = win.contentDocument.body.offsetHeight;
      parent.document.getElementById("window3").hieght = win.height;
     }else if(win.Document && win.Document.body.scrollHeight){
      win.height = win.Document.body.scrollHeight;
      win.style.height = win.height + "px";
      //测试时出现问题:ie中,style的高度没改,属性改了,框架高度没变,用
      下面的方法改动样式的高度,问题解决了。
      //parent.document.getElementById("window3").height='450px';
        }
   }
  }
  }

   function openUrl(url){
         $("#tcInfos").show();
         $("#window4").attr("src",url);//jquery iframe跳转
   }

</script>

下一篇:没有了
图片展示
 
相关评论
站长推荐