chenjian_791 发表于 2013-1-29 08:51:56

Iframe自适应

同域时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
页: [1]
查看完整版本: Iframe自适应