六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 166|回复: 0

Iframe自适应

[复制链接]

升级  76%

10

主题

10

主题

10

主题

童生

Rank: 1

积分
38
 楼主| 发表于 2013-1-29 08:51:56 | 显示全部楼层 |阅读模式
同域时Iframe高度自适应
下面的代码兼容IE/Firefox浏览器,控制id为“iframeid”的iframe的高度,通过JavaScript取得被嵌套页面最终高度,然后在主页面进行设置来实现。

代码如下,可复制。另外,请注意此解决方案仅供同域名下使用。
<script type="text/javascript">   function SetCwinHeight(){    var iframeid=document.getElementById("iframeid"); //iframe id    if (document.getElementById){     if (iframeid && !window.opera){      if (iframeid.contentDocument && iframeid.contentDocument.body.offsetHeight){       iframeid.height = iframeid.contentDocument.body.offsetHeight;      }else if(iframeid.Document && iframeid.Document.body.scrollHeight){       iframeid.height = iframeid.Document.body.scrollHeight;      }     }    }   }  </script>  <iframe width="100%" id="iframeid"  height="1" frameborder="0" src="kimi.php"></iframe>

也可以用这种方法,解决跨二级域
<script language="JavaScript" type="text/javascript">document.domain = "51.com";var mtv_height = document.body.scrollHeight;try{ document.body.onselect = document.body.onselectstart = function() { return false; }; parent.document.getElementById('mtv_iframe').style.height = mtv_height; }catch(e){}</script>

参考网站:http://www.ccvita.com/376.html
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表