2012-10-15 38 views
2

iframe有问题。如何从iframe中的文档对象获取父元素

我想从iframe的内容获取父objet或父节点。

我的代码:

a.html:

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script> 
</head> 
<body> 
<div> 

    <input type="button" value="click me" onClick="parent.getIframe(document);"/> 
</div> 
</body> 
</html> 

的index.php:

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script> 
<script> 
var getIframe = function(obj){ 
    // Iframe Element = $(obj).parent(); 
    //or some things likes this. 
} 
</script> 
</head> 
<body> 
<div> 
    <div id="myId"> 
     <iframe src="a.html"></iframe> 
    </div> 
</div> 
</body> 
</html> 

现在我想,当我点击 “点击我” 按钮。我想通过iframe的文档对象获取HTMLIFRAME对象元素。

我需要你的帮助。

+0

如果我想要得到的div id包含IFRAME(“身份识别码”),当我点击“点击我”在a.html页面,我该怎么办? –

+0

如果我想要得到div的ID包含iframe(“myId”),当我点击“点击我”在一个HTML页面,我该怎么做?现在我的解决方案是比较iframe的src属性。但我不想使用它。因为在我的页面中有很多iframe。它可以是50或50以上的iframe =>我的程序将会很慢。我觉得这不是很好的理想。当点击a.html的“点击我”按钮时。我们有页面或contentDocument或contentWindow的文档。为什么我们不能通过它们获得Element IFrame。我需要你的帮助。 –

+0

听起来像你可能会遇到[同源政策](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy)。 – simonwo

回答

2

您可以使用以下两种属性:

contentDocument - Returns the document object generated by a frame/iframe 
contentWindow - Returns the window object generated by a frame/iframe 

检查this reference

+0

谢谢安舒。但我想从contentDocument或contentWindow获取元素Iframe或iframe的文档。因为我在a.html中。所以。我有他们。我想从他们获取元素iframe。 –

相关问题