JS实现iframe自适应高度

 

项目中有时候会用到iframe标签,但是搞得会显示有问题;

纯css解决办法是,给iframe设置高度或者给iframe设置高度。。。

但是有的时候需要高度自适应,这个时候js方便一些。

 

 

<iframe width="100%" height="100%" id="main" name="main" src="abc.html" frameborder="0" onload="SetCwinHeight(this);"></iframe>

<script>
	//去掉iframe滚动条并显示全部内容
	function SetCwinHeight(iframeObj){
	    if (document.getElementById){
	        if (iframeObj && !window.opera){
	            if (iframeObj.contentDocument && iframeObj.contentDocument.body.offsetHeight){
	                iframeObj.height = iframeObj.contentDocument.body.offsetHeight;
	            }else if(document.frames[iframeObj.name].document && document.frames[iframeObj.name].document.body.scrollHeight){
	                iframeObj.height = document.frames[iframeObj.name].document.body.scrollHeight;
	            }
	        }
	    }
	}
</script>

  

 

 

 

posted @ 2014-04-14 15:52  赵小磊  阅读(218)  评论(0)    收藏  举报
回到头部