在iframe和parent之间传递jquery变量

Hus*_*ein 20 variables iframe jquery

我如何使用jQuery和iframe.获取并传递variframebodybodyiframe.我有以下示例.如何单击该按钮iframe并使其生效body #test.如何在iframe中准备var x.

<body>
var x = "whatever";
<div id"test"></div>
<iframe width="200px" height="200px" src="page.html"></iframe>
</body>
Run Code Online (Sandbox Code Playgroud)

在page.html里面我有

<button>clickme</button>
<script>
var elm = $('<span>content</span>');
    elm.appendTo('#test')
</script>
Run Code Online (Sandbox Code Playgroud)

Lia*_*amB 34

$("#myid", top.document); 
Run Code Online (Sandbox Code Playgroud)

要么

$("#myid", parent.document.body); 
Run Code Online (Sandbox Code Playgroud)

这将使您可以访问IFRAME的容器

根据:http://groups.google.com/group/jquery-en/browse_thread/thread/5997ef4a60a123af

  • 请注意,框架必须位于相同的原点才能工作.http://en.wikipedia.org/wiki/Same_origin_policy (4认同)

Spi*_*der 14

从父母的角度来看:

var iFrameValue = $('#iframe').get(0).contentWindow.myLocalFunction();
Run Code Online (Sandbox Code Playgroud)

要么

var iFrameValue = $('#iframe').get(0).contentWindow.myLocalVariable;
Run Code Online (Sandbox Code Playgroud)

从iframe的角度来看

<script type="text/javascript">

var myLocalVariable = "hello";

function myLocalFunction () {
    return "hello";
}

</script>
Run Code Online (Sandbox Code Playgroud)