如何从iframe访问主窗口元素

时间:2021-01-16 23:59:48

I hope someone can help with this issue. I have a popup iframe using lytebox. I would like it that when i click a link in my iframe with the class "mylink" that it executes the ajax call and update a div in my main window with the id "mydiv". I know how to do the onclick events and the ajax call but i do not know how to access the "mydiv" from the main window and update it with the content. Any help on this would be greatly appreciated. Thanks.

我希望有人可以帮助解决这个问题。我有一个使用lytebox的弹出iframe。我希望当我点击我的iframe中的一个带有“mylink”类的链接时,它执行ajax调用并在我的主窗口中用id“mydiv”更新一个div。我知道如何进行onclick事件和ajax调用,但我不知道如何从主窗口访问“mydiv”并使用内容更新它。任何有关这方面的帮助将不胜感激。谢谢。

1 个解决方案

#1


You can simply use window.parent.document to access the parent document from the iframe.

您只需使用window.parent.document从iframe访问父文档即可。

jQuery allows you to specify a context for searches, so you could then do:

jQuery允许您为搜索指定上下文,因此您可以执行以下操作:

$(myselector, window.parent.document);

#1


You can simply use window.parent.document to access the parent document from the iframe.

您只需使用window.parent.document从iframe访问父文档即可。

jQuery allows you to specify a context for searches, so you could then do:

jQuery允许您为搜索指定上下文,因此您可以执行以下操作:

$(myselector, window.parent.document);