ITeye博客

iframe 自适应高度 IE FireFox兼容

keda413 · 2009-04-14 00:47 · javascript · C · C++ · C# · Firefox · IE · Markdown

方法一:
function c()

{

var frm = document.getElementById("IframeId");
//document.frames["IframeName"].document获取子页面的document对象,可获得页面的各个对象,但是不能得到iframe标签的属性
document.getElementById("IframeUser")只是得到iframe标签对象,可获得标签的各个属性,但不能得到子页面的各个对象。document.frames为IE下使用,frm.contentDocument为firefox下使用。
var subWeb = document.frames ? document.frames["IframeUserName"].document : frm.contentDocument;

if(frm != null && subWeb != null)

{ frm.height = subWeb.body.scrollHeight;}

} 

方法二:
function d(){
 var frm=document.getElementById("IframeUser");
//frm.contentWindow.document为IE下 使用,获得子页面各个对象
var sub=frm.contentDocument ? frm.contentDocument:frm.contentWindow.document; 
 if(frm!=null&&sub!=null){
  frm.height=sub.body.scrollHeight;
 }
}

 

<iframe marginwidth=0 marginheight=0 src="2.html"   onload="d();" width="100%"    id="IframeId" name="IframeName"    frameborder="0"   scrolling="no"></iframe> 

 

评论(1)

  1. #1 楼 · izat · 2009-11-22 18:14

    多谢 找了一下发现大部分论坛的人貌似从来不用firefox的,给出的代码都只ie适用

评论功能维护中,当前仅展示历史评论。