我正在尝试使用 iframe 自动高度,最适合我的代码是:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>THE IFRAME HOLDER</title>
<script>
function alertsize(pixels){
pixels+=32;
document.getElementById('myiframe').style.height = pixels+"px";
}
</script>
</head>
<body style="background:silver;">
<iframe src='theiframe.htm' style='width:458px;background:white;' frameborder='0' id="myiframe" scrolling="auto"></iframe>
</body>
</html>
iframe:以名称“theiframe.htm”保存
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>IFRAME CONTENT</title>
<script>
function toggle(obj) {
var el = document.getElementById(obj);
if ( el.style.display != 'block' ) el.style.display = 'block';
else el.style.display = 'none';
parent.alertsize(document.body.scrollHeight);
}
</script>
</head>
<body onload="parent.alertsize(document.body.scrollHeight); " >
<a href="javascript:toggle('moreheight')">toggle height?</a><br />
<div style="display:none;" id="moreheight">
more height!<br />
more height!<br />
more height!<br />
more height!<br />
more height!<br />
more height!<br />
more height!<br />
more height!<br />
more height!<br />
more height!<br />
more height!<br />
</div>
text<br />
text<br />
text<br />
text<br />
text<br />
text<br />
text<br />
text<br />
THE END
<div style="clear:both;"></div>
</body>
</html>
在 IE、Opera 和 Firefox 中,它可以完美运行。在 Chrome 和 Safari 中,似乎不起作用。有朋友说问题可能出在这里:
onload="parent.alertsize(document.body.scrollHeight);"
Chrome 中的示例: http ://www.warezexpress.com/ups/2009_setembro/chrome.jpg
Safari 中的示例:(iframe 扩展,并且不减小) http://www.warezexpress.com/ups/2009_setembro/safari.jpg
但我仍然不明白 JavaScript 来解决这个问题。我在论坛中准备了这段代码。谢谢你的话!