打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
完美iframe自适应宽度和高度
在ie下能实现的功能,对照着FF下的方法与属性修改就行了。今天终于把这个问题彻底解决了,记录一下。

  感谢http://www.cnblogs.com/Cheese_Ren/

  js代码如下:

  代码

  //iframe高度自适应

  function IFrameReSize(iframename) {

  var pTar = document.getElementById(iframename);

  if (pTar) {  //ff

  if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight) {

  pTar.height = pTar.contentDocument.body.offsetHeight;

  } //ie

  else if (pTar.Document && pTar.Document.body.scrollHeight) {

  pTar.height = pTar.Document.body.scrollHeight;

  }

  }

  }

  //iframe宽度自适应

  function IFrameReSizeWidth(iframename) {

  var pTar = document.getElementById(iframename);

  if (pTar) {  //ff

  if (pTar.contentDocument && pTar.contentDocument.body.offsetWidth) {

  pTar.width = pTar.contentDocument.body.offsetWidth;

  }  //ie

  else if (pTar.Document && pTar.Document.body.scrollWidth) {

  pTar.width = pTar.Document.body.scrollWidth;

  }

  }

  }

  使用方法如下:

  <iframe src="Main.aspx" scrolling="no" frameborder="0" height="100%" id="mainFrame" width="100%" onload='IFrameReSize("mainFrame");IFrameReSizeWidth("mainFrame");'></iframe>

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
用javascript动态调整iframe高度
Iframe自设定高度和宽度
创建高度动态变化的Iframe
收藏一些有用的JS代码
关于contentWindow和contentDocument
关于IFRAME 自适应高度的研究
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服