2009-12-24 57 views
0

请帮我,自动iframe的高度

的第一次iframe的高度正在改变,因为每所装载的高度,但在iframe的内容更改为更大的高度比现在又一次地降低其不具有新的价值。

例如 加载iframe的高度= 1386px 新的内容变化iframe的高度= 3440px 并再次更改到新的高度= 3440px(但实际高度是非常小的比1300px)

这就是我试图

$('iframe').load(function() 
     { 
      document.getElementById('loaderIFRAME').style.display = 'none'; 
      document.getElementById('myiframe').style.display = 'block'; 
      // Set inline style to equal the body height of the iframed content. 

      if($.browser.msie){ 
       this.style.height = this.contentWindow.document.body.scrollHeight + 'px'; 
      }else{ 
       this.style.height = this.contentWindow.document.body.scrollHeight + 'px'; 
      } 
     } 

,另一个也同样

function autoIframe(frameId) { 
try{ 
     frame = document.getElementById(frameId); 
     innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document; 
     objToResize = (frame.style) ? frame.style : frame; 
     objToResize.height = innerDoc.body.scrollHeight + 10+ 'px'; 
     alert(objToResize.height); 
    } 
    catch(err){ 
     window.status = err.message; 
    } 
} 

如何能为好吗?

回答

2

问题是,当您将iframe高度设置为较大的值时,即使真实文档高度较小,其scrollHeight总是等于较大的值。您可以尝试将iframe高度设置为1px,然后阅读scrollHeight属性。

$('iframe').load(function(){ 
    try { 
     var body = $(this).contents().find('body'); 
     this.height = 1; 
     this.height = body.attr('scrollHeight'); 
    } catch(e){} 
}); 

有时最好使用offsetHeight + offsetTop属性。 Here你可以找到我的旧脚本执行。