该代码适用于同一域内的iframe操作
iframe框架的代码如下(父)
<script> function setIframeHeight(iframe) { if (iframe) { var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow; if (iframeWin.document.body) { iframe.height = iframeWin.document.documentElement.scrollHeight ||iframeWin.document.body.scrollHeight; } } }; window.onload = function () { setIframeHeight(document.getElementById('iframe1')); }; </script> <iframe id="iframe1" name="iframe1" onload="setIframeHeight(this)" scrolling="no" src="" width="100%" height="100%" frameborder="0"></iframe>
iframe框架包含页的代码(子)
<script type="text/javascript"> $(document).ready(function(){ var doc = document.body; doc.onclick = function() { var _height=$("#div_name").css("height"); //#div_name换成你子页面需要监听的div名称 window.parent.$("#iframe1").css("height",_height); }; }); </script>
好了以上代码就可以实现父页面的高度可以随时根据子页面的折叠或展开的高度而变换了。