当前位置:Gxlcms > JavaScript > javascript装载iframe子页面,自适应高度_javascript技巧

javascript装载iframe子页面,自适应高度_javascript技巧

时间:2021-07-01 10:21:17 帮助过:3人阅读

假设主页面有一个div,里面放置一个iframe
代码如下:





3个菜单链接,分别在iframe加载 1.html、2.html、3.html 三个页面。

3个子页面分别在自己页面加载完window.onload执行
代码如下:

function aa(){
var newHeight = document.body.scrollHeight + 20 + "px";
window.parent.document.getElementById("frameBox").style.height = newHeight;
//以上firefox通过,但是ie6必须加上下面这句,不然iframe高度是改了,但是可见区域没有改
window.parent.document.getElementById("frameWin").style.height = newHeight;
}

以下方法只需要把代码放在主页面:
页面代码:
代码如下:





js脚本(加在主页面):
代码如下:

function test2(){
var frameWin = document.getElementById("frameWin");
var frameBox = document.getElementById("frameBox");
var newHeight;
if (frameWin.Document){
newHeight = frameWin.Document.body.scrollHeight + 20 + "px";
}else{
newHeight = frameWin.contentDocument.body.scrollHeight+ 20 + "px";
}
frameWin.style.height = newHeight;
frameBox.style.height = newHeight;
}

人气教程排行