2013-09-28 74 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/>