2013-09-28 104 views
0

我想從我的iframe裏面設置div的高度。如何從iframe元素更改元素屬性?

我有類似

<div id='test' style='height:150px;'>some stuff..</div> 

<iframe id="test" src="http://www.myproject.com frameborder="0" scrolling="no"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>test project</title> 
    <link rel="stylesheet" type="text/css" href="g_reset.css"> 
    <script type="text/javascript" src="options.js"></script> 
    </head> 
    bunch of html... 
    <a id='click' href='#'>click<a/> 
</iframe> 

我希望能夠改變「測試」 DIV高度,當我點擊「點擊」我的iframe內。

options.js我..

$('#click).click(function(){ 
    alert('pop') 
    $('#test').attr('height','500'); 
}) 

警報會彈出,但它似乎並沒有改變div的高度。任何人都可以幫我解決這個問題嗎?非常感謝!

+0

iframe是否與實現iframe的主窗口相同的域/端口/協議加載? –

+0

@StevenV是的。它在同一個域中。 – FlyingCat

回答

1

作爲一個jQuery的解決方案,您可以在iframe中使用這樣的事情。

$('#test', window.parent.document).css('height', '500px'); 

$(window.parent.document).find('#test').css('height', '500px'); 

對於第一個例子,在$()第二個參數是在其中進行搜索的上下文。另外請注意,我使用css()而不是attr(),因爲您試圖修改元素的樣式,而不是屬性。

0

試試這個:d

<a id='click' href='#' onClick='parent.document.getElementById(\"test\").style.height=\"50px\"'>click<a/>