iframe高度自适应

旧街凉风 提交于 2020-01-30 08:51:02
 1 var adjustIframe = function (id) {
 2     var iframe = document.getElementById(id)
 3     var idoc = iframe.contentWindow && iframe.contentWindow.document || iframe.contentDocument;
 4     var callback = function () {
 5         var iheight = Math.max(idoc.body.scrollHeight, idoc.documentElement.scrollHeight); //取得其高
 6         iframe.style.height = iheight + "px";
 7     }
 8     if (iframe.attachEvent) {
 9         iframe.attachEvent("onload", callback);
10     } else {
11         iframe.onload = callback
12     }
13 }
1 <iframe id="js_sub_web" width="80%" frameborder="0" scrolling="no" allowTransparency="true" src="http://www.cnblogs.com/rubylouvre/archive/2009/09/15/1566722.html">
2 </iframe>

使用方法:

1 window.onload = function(){
2      adjustIframe("js_sub_web");
3 }

文章来源:http://www.cnblogs.com/rubylouvre/archive/2011/01/05/1926226.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!