原文可以访问下面连接:http://blog.csdn.net/meizz/archive/2006/03/27/639857.aspx
代码<script type="text/javascript" language="javascript">
function iframeAutoFit() {
try {
if (window != parent) {
var frame = parent.document.getElementById("contentIframe");
if (frame.contentWindow == window) {
var h1 = 0, h2 = 0, d = document, dd = d.documentElement;
frame.parentNode.style.height = frame.offsetHeight + "px";
frame.style.height = "10px";
if (dd && dd.scrollHeight) h1 = dd.scrollHeight;
if (d.body) h2 = d.body.scrollHeight;
var h = Math.max(h1, h2);
if (document.all) { h += 4; }
if (window.opera) { h += 1; }
frame.style.height = frame.parentNode.style.height = h + "px";
}
}
}
catch (ex) { }
}
if (window.attachEvent) {
window.attachEvent("onload", iframeAutoFit);
//window.attachEvent("onresize", iframeAutoFit);
}
else if (window.addEventListener) {
window.addEventListener('load', iframeAutoFit, false);
//window.addEventListener('resize', iframeAutoFit, false);
}
</script>
function iframeAutoFit() {
try {
if (window != parent) {
var frame = parent.document.getElementById("contentIframe");
if (frame.contentWindow == window) {
var h1 = 0, h2 = 0, d = document, dd = d.documentElement;
frame.parentNode.style.height = frame.offsetHeight + "px";
frame.style.height = "10px";
if (dd && dd.scrollHeight) h1 = dd.scrollHeight;
if (d.body) h2 = d.body.scrollHeight;
var h = Math.max(h1, h2);
if (document.all) { h += 4; }
if (window.opera) { h += 1; }
frame.style.height = frame.parentNode.style.height = h + "px";
}
}
}
catch (ex) { }
}
if (window.attachEvent) {
window.attachEvent("onload", iframeAutoFit);
//window.attachEvent("onresize", iframeAutoFit);
}
else if (window.addEventListener) {
window.addEventListener('load', iframeAutoFit, false);
//window.addEventListener('resize', iframeAutoFit, false);
}
</script>
来源:https://www.cnblogs.com/wuwei_chen/archive/2010/02/17/1668946.html