Access iframe's “unnamed” parent

2019-08-05 10:21发布

问题:

If I had some thing like this:

<div id="parent">
<iframe....></iframe>
</div>

I could: window.parent.document.getElementById('parent').innerHTML. But I have something like this:

<div>
<iframe....></iframe>
</div>

Is there anyway of accessing this? I might have anywhere up to 20 iframes on a given page, and would prefer not to make 20 individual iframe pages.

Thanks, Jason

回答1:

window.frameElement is a reference to the IFRAME element containing the current page. You can use window.frameElement.parentNode (inside of the IFRAME) to get the unnamed parent element.

Be aware that the property is non-standard. frameElement seems to be supported in Firefox, Opera, and IE.



回答2:

so im assuming you control the the parents code

then you can iterate true all the iframes and give them a reference to the parent div to use.



回答3:

I think this will help you: document.getElementsByTagName("iframe").

W3School's Page

This method will return a vector with all elements with tag it found in the page.