动态改变iframe的高度

网上找了很久,发现都不能实现缩小iframe高度,自己修改了一下,发现原来是js添加了height以后,再取时就都是这个值了。所以只要做如下的修改就行了,IE6+,FF都有用。

 1 function changeHeight(obj) {
 2     var cwin=document.getElementById("frameId");
 3     cwin.removeAttribute('height');
 4     var iframeHeight = 0;
 5     if (document.getElementById) {
 6         if (cwin && !window.opera) {
 7             if (cwin.contentDocument && cwin.contentDocument.body.scrollHeight)
 8                 iframeHeight = cwin.contentDocument.body.scrollHeight + 10; //FF NS
 9             else if(cwin.Document && cwin.Document.body.scrollHeight)
10                 iframeHeight = cwin.Document.body.scrollHeight + 10;//IE
11         } else {
12             if(cwin.contentWindow.document && cwin.contentWindow.document.body.scrollHeight)
13                 iframeHeight = cwin.contentWindow.document.body.scrollHeight + 10;//Opera
14         }
15     }     
16     document.getElementById('frameId').height=iframeHeight;

17 } 

原文地址:https://www.cnblogs.com/zhuyoufeng/p/2196140.html