iframe 结构如下
<iframe src="index.html" id="frame" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" onLoad="iFrameHeight()"></iframe>
js 实现
<script type="text/javascript" language="javascript"> function iFrameHeight() { var ifm = document.getElementById("frame"); var subWeb = document.frames ? document.frames["frame"].document : ifm.contentDocument; if (ifm != null && subWeb != null) { ifm.height = subWeb.body.scrollHeight; ifm.width = subWeb.body.scrollWidth; } } </script>
jQuery 实现
$("#frame").load(function(){ var mainheight = $(this).contents().find("body").height()+30; $(this).height(mainheight); });