iframe 自适应高度

方法一  在父页面写入js方法setHeight,iframe加载子页面时,子页面加载父页面方法setHeight.self.parent.setHeight();
]
function setHeight(){
	  var mainObj = document.getElementById("main");
	  var d_height=mainObj.contentDocument.body.clientHeight;
	  if(d_height<230){
		d_height=230;
	  }
	  mainObj.height=d_height+"px";
}



方法二 父页面写入js方法SetCwinHeight,在iframe中使用onload属性加载方法SetCwinHeight. (onload="SetCwinHeight(this)"
function SetCwinHeight(obj)
{
  var cwin=obj;
  if (document.getElementById)
  {
	if (cwin && !window.opera)
	{
	if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight)
	cwin.height = cwin.contentDocument.body.offsetHeight + 20; //FF NS
	else if(cwin.Document && cwin.Document.body.scrollHeight)
	cwin.height = cwin.Document.body.scrollHeight + 10;//IE
	}
	else
	{
	if(cwin.contentWindow.document && cwin.contentWindow.document.body.scrollHeight)
	cwin.height = cwin.contentWindow.document.body.scrollHeight;//Opera
	}
  }
 
} 


你可能感兴趣的:(JavaScript)