2009-12-24 59 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你可以找到我的舊腳本執行。