iframe 自适应高度

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:44   2526   0
[color=red][b]方法一[/b][/color] [size=large]在父页面写入js方法setHeight,iframe加载子页面时,子页面加载父页面方法setHeight.[color=red]self.parent.setHeight();[/color][/size]
]

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";
}



[color=red][b]方法二[/b][/color] [size=large] 父页面写入js方法SetCwinHeight,在iframe中使用onload属性加载方法SetCwinHeight. ([color=red]οnlοad="SetCwinHeight(this)"[/color])[/size]。

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
}
}

}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP