在 iframe 和父级之间传递 jquery 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4689145/
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
Pass jquery variables between iframe and parent
提问by Hussein
How can I work with jQuery and iframe
. Get and pass var
from iframe
to body
and from body
to iframe
. I have the following example. How can I click the button in iframe
and make it take effect on the the body
#test
. How about ready var x in iframe.
我如何使用 jQuery 和iframe
. 获取并传递var
从iframe
到body
和body
到iframe
。我有以下示例。如何单击按钮iframe
并使其在body
#test
. 在 iframe 中准备好 var x 怎么样。
<body>
var x = "whatever";
<div id"test"></div>
<iframe width="200px" height="200px" src="page.html"></iframe>
</body>
Inside page.html i have
里面 page.html 我有
<button>clickme</button>
<script>
var elm = $('<span>content</span>');
elm.appendTo('#test')
</script>
回答by LiamB
$("#myid", top.document);
or
或者
$("#myid", parent.document.body);
This will give you access to the container of the IFRAME
这将使您可以访问 IFRAME 的容器
as per : http://groups.google.com/group/jquery-en/browse_thread/thread/5997ef4a60a123af
根据:http: //groups.google.com/group/jquery-en/browse_thread/thread/5997ef4a60a123af
回答by sparkyspider
From the parents perspective:
从家长的角度:
var iFrameValue = $('#iframe').get(0).contentWindow.myLocalFunction();
OR
或者
var iFrameValue = $('#iframe').get(0).contentWindow.myLocalVariable;
From the iframe perspective
从 iframe 的角度来看
<script type="text/javascript">
var myLocalVariable = "hello";
function myLocalFunction () {
return "hello";
}
</script>
回答by Salman A
You can use contentWindow
and contentDocument
properties, if same origin poilicyis not a problem. I've setup a demo. View code for the demoand code for iframe.
如果同源策略没有问题,您可以使用contentWindow
和contentDocument
属性。我已经设置了一个演示。查看该演示代码和代码的iframe。