2014-01-20 92 views
0

我想使用iframe在彈出窗口中顯示遠程URL的內容或網站。但我不想修復iframe的高度。我已經使用了一些代碼使用動態高度創建iframe

function resizeIframe(iframe) { 
    iframe.height = iframe.contentWindow.document.body.scrollHeight + "px"; 
} 

,我曾嘗試另一個代碼

document.getElementById('iframe1').scrollHeight(); 

但是,這是行不通的。 請建議

+0

檢查我的答案在http://stackoverflow.com/questions/20541182/html5-resize-top-level-document-iframe-from-inside-a-nested-iframe/20541967# 20541967 – RononDex

+0

已檢查,但不適用於我。 –

回答

1

試試這個:

newheight = document.getElementById(iframe1).contentWindow.document.body.scrollHeight; 
//For Firefox: 
document.getElementById(id).height = (newheight) + "px"; 

//For Other: 
$('#' + iframe1).css('min-height', newheight + 'px'); 
+0

var newheight = document.getElementById('iframe1')。contentWindow.document.body.scrollHeight; alert(newheight);但它不起作用 –

+0

最新錯誤 –

1

我想你錯過樣式屬性:

iframe.style.height = iframe.contentWindow.document.body.scrollHeight + "px";

或第二,我認爲你應該做這樣的事情

$('#iframe1').height(300); //will set your Iframe height with id iframe1 to 300px

否則

$('#iframe1').css("height", "300px") //do the same

+0

如果您使用第二個或第三個解決方案,請記住在您的頁面中包含jQuery! – Morrisda

+0

var newheight = iframe.contentWindow.document.body.scrollHeight; alert(newheight);但它不起作用 –

+0

Newheight = Iframe.contentWindow.document.body.offsetHeight +'px';嘗試一下。現在我無法測試,我是來自移動的抱歉。 @devjohn – Morrisda