从 iframe 访问和更改父页面(使用 jquery) [英] access and change parent page from iframe (with jquery)
问题描述
有什么方法可以从 iframe 访问父页面(并更改父页面)?
is there any way how to access from iframe to parrent page (and change parrent page)?
<body>
<iframe src="frame1.html" name="frame1" height="100%"></iframe>
<div id="test1"></div>
</body>
在 frame1.html 中是 并且我想将文本
"添加到 clicked
<div id="test1"></div>
,当 被点击时.
In frame1.html is <a href=..>
and I want to add text "<h1>clicked</h1>
" into <div id="test1"></div>
, when the <a href..>
was clicked.
谢谢.
推荐答案
如果你的子页面(frame1.html)和父页面在同一个域,你可以在子窗口写如下代码:
If your child page (frame1.html) is located at the same domain as the parent page, You can write a code like below in the child window :
$('#test1', parent.document).html('<h1>clicked</h1>');
第二个参数提供了 context
,在其中搜索与第一个参数匹配的元素.文档在这里:http://api.jquery.com/jQuery/#jQuery-选择器上下文
The second parameter provides the context
in which to search the element matched by the first parameter. The Document is here:http://api.jquery.com/jQuery/#jQuery-selector-context
jQuery( selector [, context ] )
因此,您的代码 (frame1.html) 可能如下所示:
So, your code (frame1.html) could go like this:
<a href="..."
onclick="$('#test1', parent.document).html('<h1>clicked</h1>');">click me</a>
希望这会有所帮助.
这篇关于从 iframe 访问和更改父页面(使用 jquery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!