2012-03-27 55 views
0

我該如何做到這一點?調整列中的IFRAME(HTML)

我在列(html表)內有一個iframe,但我想使td與iframe中的文檔一樣高。

可能嗎?

+0

嘗試 – 2012-03-27 15:41:47

+0

td應自動調整大小。問題是iframe。如果你設置了一個明確的高度,它應該工作。但是,如果需要自動調整大小,您可能需要更多的工作。 – Timmerz 2012-03-27 15:43:32

+0

JS的使用是否正常? – 2012-03-27 15:45:00

回答

2

這是答案(在JQuery中)。

jQuery中:

$("#myIframe").on("load", function() 
{ 
    $(this).parent("td:first").css('width', $(this).css('width')).css('height', $(this).css('height')) 
}); 
+0

從來沒有試過Jquery,我現在就試試看,看看會發生什麼 – Bachask8 2012-03-27 17:41:48

+0

我需要把這段代碼放在哪裏?,srry,因爲你可以看到新的Jquery(我有庫和所有在我的專家中) – Bachask8 2012-03-27 17:48:09

0

我使用這段代碼,它工作確定爲我。我不知道是否有更好的方法來實現這一點(請注意,這段代碼有一個setInterval,它會每隔1秒檢查iframe的內容高度並更新它的高度)。它也使用jQuery。

let iframe = document.getElementById('your_iframe_id'), 
    current_height = 0, 
    iframe_content = $(iframe).contents().find('body'); 

setInterval(() => { 
    let new_height = iframe.contents().find('body').height(); 
    current_height = new_frame_height(current_height, new_height); 
    iframe.css({ height: current_height }); 
}, 1000) 

function new_frame_height(last_height, height) { 
    if(height != last_height + 60) { 
     last_height = height; 
    } 
    return last_height + 60 
} 

順便說一句,我認爲這隻適用於iframe是否在父母同一個域。 如果不是,則來自瀏覽器的交叉源塊將不允許父母讀取兒童的內容。