我有一个iframe,其中包含一个包含多个页面的脚本,我想根据内容高度自动调整此iframe的高度。当我加载第一页时它正常工作但当我换到另一页时,再次调用该函数,但即使内容在每个页面上的高度不同,高度也是相同的。
javascript函数:
function autoResize(id){
var newheight;
if(document.getElementById){
newheight=document.getElementById(id).contentWindow.document.body.scrollHeight;
}
document.getElementById(id).height= (newheight) + "px";
}
我的iframe:
<iframe id="iframe" src="myScript" width="950px" onLoad="autoResize('iframe');"></iframe>
当我使用alert(newheight)
设置后添加contentWindow.document.body.scrollHeight
时,我会在每个页面上收到提醒,但高度始终相同。
有没有其他方法可以获得当前的iframe内容高度?
答案 0 :(得分:0)
您可以尝试使用此功能,在每个iframe加载时传递iframe
ID,iframe id
应该是唯一的
function autoResize(frameId){
var F = document.getElementById(frameId);
if(F.contentDocument) {
F.height = F.contentDocument.documentElement.scrollHeight; //FF 3.0.11, Opera 9.63, and Chrome
} else {
F.height = F.contentWindow.document.body.scrollHeight; //IE6, IE7 and Chrome
}
}
答案 1 :(得分:0)
试试这个:
function autoResize(id){
var n1=id.contentWindow.document.body.scrollHeight;
document.getElementById(id).style.height = n1 + 'px';
}