jQuery 从 iframe 中选择 iframe 父级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12477111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select iframe parent from within iframe
提问by Albert
I have an iframe which does picture uploading to give a false sense of Ajax uploading.
我有一个 iframe,它可以上传图片,给人一种 Ajax 上传的错误感觉。
This iframe's content has javascript which checks various things and sends status. See it as a progress report almost. Anyway, my page that contains the iframe, is the one showing the data and what not, so I want to change the data on the parent page, from within the iframe. Here is my code:
这个 iframe 的内容有 javascript,它检查各种事情并发送状态。几乎把它看成一个进度报告。无论如何,我的包含 iframe 的页面是显示数据的页面,而不是显示数据的页面,所以我想从 iframe 中更改父页面上的数据。这是我的代码:
<div id="iframe_response"></div>
<iframe style="border:none" src="someurl" width="100%" height="350px" id="iframe"></iframe>
And here is my jQuery in the iframe:
这是我在 iframe 中的 jQuery:
$('#iframe_response', "#iframe").append('this should go there but it\'s not...');
It does however not write anything to that div. I have called it on document.ready, but alas...
但是,它不会向该 div 写入任何内容。我已经在 document.ready 上调用了它,但是唉...
What am I doing wrong?
我究竟做错了什么?
采纳答案by Shannon
Here's the answer! It's just a little confusing to start with but really easy!
这就是答案!刚开始有点混乱,但真的很容易!
Assuming that same origin policyis not a problem, you can use parent.document to access the elements, and manipulate them.
假设同源策略没有问题,您可以使用 parent.document 访问元素并操作它们。
Demo: Here
Iframe Outer: Here
Iframe Inner: Here
Hope this helps, Spent I fair bit of time figuring this out for you so I'd love the support of upping my answer!
希望这会有所帮助,我花了很多时间为您解决这个问题,所以我很乐意支持我的答案!
Cheers, Shannon
干杯,香农
回答by Steven Pribilinskiy
use parent.document
instead of window.parent.document
使用parent.document
代替window.parent.document
$(parent.document).find('#whatever')
回答by makim
I had the same issue and none of the above solutions (jquery) did work for me.
我遇到了同样的问题,上述解决方案(jquery)都不适合我。
After a little bit of trial and error this worked for me:
经过一些试验和错误后,这对我有用:
(But beware, this will only work if parent also supports Jquery)
(但要注意,这只有在父级也支持 Jquery 时才有效)
window.parent.$('#iframe_response').html('this should go there but it\'s not...');
回答by Yograj Gupta
You should try this
你应该试试这个
$(window.parent).find('#iframe_response').html('this should go there but it\'s not...');