日期:2014-05-16  浏览次数:20308 次

iframe自动调节大小问题--救命!
我页面中iframe的src可以选择的,但是由于每次选择的src页面的大小不确定,所以要不断地改变iframe的大小,并且我不愿意用滚动栏,我已经把iframe的滚动栏设为none了。
能不能写段代码让iframe根据所容纳页面的大小自动设置高度呢?
很急,救命!!!!!!!!!!

------解决方案--------------------
主页面:
先 <iframe id= "frameName " src= 'aaa.htm '> </iframe>

内嵌页:aaa.htm

<body onload= "parent.document.getElementById( 'frameName ').height=document.body.scrollHeight; ">
------解决方案--------------------
每查询出一次结果,加上这句
document.getElementById( 'frameName ').height=document.getElementById( 'frameName ').scrollHeight;
------解决方案--------------------
<script language= "Javascript ">
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf( "Firefox ")).split( "/ ")[1];
//extra height in px to add to iframe in FireFox 1.0+ browsers
var FFextraHeight=getFFVersion> =0.1? 16 : 0 ;

function dyniframesize(iframename)
{
var pTar = null;
if (document.getElementById)
{
pTar = document.getElementById(iframename);
}
else
{
eval( 'pTar = ' + iframename + '; ');
}
if (pTar && !window.opera)
{
//begin resizing iframe
pTar.style.display= "block ";

if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight)
{
//ns6 syntax
pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight;
}
else if (pTar.Document && pTar.Document.body.scrollHeight)
{
//ie5+ syntax
pTar.height = pTar.Document.body.scrollHeight;
}
}
}


</script>

<iframe id= "contentframe " border= "0 " name= "content " frameSpacing= "0 " src= "aaa.html "
scrolling= "no " onload= "javascript:{dyniframesize( 'contentframe ');} " height= "100% "
allowTransparency> </iframe>
------解决方案--------------------
<html>
<head>
<meta http-equiv= 'Content-Type ' content= 'text/html; charset=gb2312 ' />
<title> iframe自适应加载的页面高度 </title>
</head>

<body>
<div> <iframe src= "child.htm "> </iframe> </div>
</body>
</html>

child.htm:

<html>
<head>
<meta http-equiv= 'Content-Type ' content= 'text/html; charset=gb2312 ' />
<title> iframe 自适应其加载的网页(多浏览器兼容) </title>
<script type= "text/javascript ">
<!--
function iframeAutoFit()
{
try
{
if(window!=parent)
{
var a = parent.document.getElementsByTagName( "IFRAME ");
for(var i=0; i <a.length; i++)
{
if(a[i].contentWindow==window)
{
var h1=0, h2=0;
a[i].parentNode.style.height = a[i].offsetHeight + "px ";
a[i].style.height = "10px ";
if(document.documentElement&&document.documentElement.scrollHeight)