2012-11-05 13 views
1

我目前正在爲我的網站編寫一個新主題,並且遇到了動態調整iframe中div元素高度的問題。 iframe中的源代碼位於我的域中,但無法直接編輯它。在iframe中訪問和調整div的高度

我想要做的是有一個腳本,它將讀取iframe的高度(例如1000px),讀取iframe中兩個div的高度(例如#content height = 200px和#question height = 600px )。然後調整這兩個高度以適合iframe頁面。

,所以我想的東西,將是這樣的:

var height = $('iframe').height(); 
var content_height = $('iframe').contents().find('#content').height(); 
var question_height = $('iframe').contents().find('#question').height(); 
var diff = height - content_height - question_height 
if diff > 0 
    "new content height" = content_height + diff/2 
    "new question height" = content_question + diff/2 
end 

我是一個完整的noob當涉及到JavaScript,因此任何幫助將是巨大的。

+0

給iframe和div一個ID ..並使用yourid.height()... – bipen

+1

您正在使用jQuery?然後你可以做到這一點,它只是選擇器和'height()'方法。嘗試一下! – Bergi

+0

剛纔我會說一說。感謝您的快速回復。 – lwm

回答

0

像這樣的東西應該工作:

$('iframe').height(); 
$('iframe').contents().find('#content').height(); 
$('iframe').contents().find('#question').height(); 
+0

在這種情況下,'iframe'可以成爲頁面上任何iframe的概念嗎?我是否需要做這樣的事情... – lwm

+0

if if more iframe than you can use id for specific one –

+0

如何在if語句之後編寫零件? – lwm

1

見Rajesh的答案。

對於if語句:不要忘記括號(以及所有分號)。

if (diff > 0)