我嘗試用jQuery和JS調整iframe的大小。首先,我從iframe列表中獲取iframe,並在iframe內容準備就緒時調整其大小。jQuery/JS調整iframe不工作的大小
NOTE: The two-steps resize is necessary because otherwise after the content of the iframe-page is a huge space.
問題:在while循環我檢查iframe的內容準備好,當我沒有設置1秒超時。但jQuery不檢查內容是否準備好,它總是進入if,並嘗試調整iframe的大小,但因爲jQuery無法獲得NULL元素的大小而失敗。
有人對我的問題有解決方案嗎?
我的代碼:
$(document).ready(function() {
var iframes = $(".my-iframe");
$.each(iframes, function() {
resizeIframe(this);
});
});
function resizeIframe(iframe) {
$(iframe).width("100%");
var iframeIsReady = false;
do
{
if ($(iframe).ready)
{
var iframeHeight = iframe.contentDocument.body.scrollHeight;
$(iframe).height(iframeHeight);
var iframeContentHeight = $(iframe).children("#DivInPage").height();
$(iframe).height(iframeContentHeight);
iframeIsReady = true;
}
else
{
setTimeout(resizeIframe(iframe), 1000);
}
}while(!iframeIsReady);
}
Edit:
Check out my solution
一個問題我看到的是,在你的setTimeout調用你想傳遞給resizeIframe來電,但你調用它,而不是,傳遞的結果。你需要'setTimeout(function(){resizeIframe(iframe);},1000);' –
你的iframe是否與你的頁面在同一個域上? –
@Pekka웃是的,但沒有可能在我的頁面中包含iframe的內容而沒有iframe – Yannik