欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iframe自适应高度和宽度

程序员文章站 2022-07-14 22:02:35
...
<div>
  <iframe id="myframe" marginHeight=0 marginWidth=0 frameBorder=0 src="innerframe.html" onload="IFrameResize()"></iframe>
</div>

在Chrome中不行,其他浏览器都可以!

function IFrameResize()
{
	var iframe = document.getElementById("myframe"); 
	var iframeDocument = null;
	//safari和chrome都是webkit内核的浏览器,但是webkit可以,chrome不可以
	if (iframe.contentDocument)
	{ 
		//ie 8,ff,opera,safari
		iframeDocument = iframe.contentDocument;
	} 
	else if (iframe.contentWindow) 
	{ 
		// for IE, 6 and 7:
		iframeDocument = iframe.contentWindow.document;
	} 
	if (!!iframeDocument) {
		iframe.width=iframeDocument.documentElement.scrollWidth+"px";
		iframe.height=iframeDocument.documentElement.scrollHeight+"px";		
	} else {
		alert("this browser doesn't seem to support the iframe document object");
	} 

}

转载于:https://www.cnblogs.com/frouds/archive/2010/06/20/1761575.html