【问题标题】:Pass jquery variables between iframe and parent在 iframe 和父级之间传递 jquery 变量
【发布时间】:2011-06-09 00:24:07
【问题描述】:

如何使用 jQuery 和 iframe。从iframebody 和从bodyiframe 获取并传递var。我有以下示例。如何点击iframe中的按钮,使其在body#test上生效。在 iframe 中准备好 var x 怎么样。

<body>
var x = "whatever";
<div id"test"></div>
<iframe width="200px" height="200px" src="page.html"></iframe>
</body>

在page.html里面我有

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

【问题讨论】:

  • 请注意,根据同源策略,当两个文档来自完全相同的域时,大多数浏览器只允许在 iframe 内操作父文档。
  • @tdammers - 这是一个很好的观点。
  • 是的。咬我一次。我有一个很棒的脚本,可以根据内容调整 iframe 的高度,但它只适用于测试平台,两个页面都是从 localhost 提供的:-(
  • 谁能告诉我如何动态传递 src 的值?

标签: jquery variables iframe


【解决方案1】:
$("#myid", top.document); 

$("#myid", parent.document.body); 

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

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

【讨论】:

【解决方案2】:

从父母的角度来看:

var iFrameValue = $('#iframe').get(0).contentWindow.myLocalFunction();

var iFrameValue = $('#iframe').get(0).contentWindow.myLocalVariable;

从 iframe 的角度来看

<script type="text/javascript">

var myLocalVariable = "hello";

function myLocalFunction () {
    return "hello";
}

</script>

【讨论】:

    【解决方案3】:

    如果same origin poilicy 没有问题,您可以使用contentWindowcontentDocument 属性。我已经设置了一个demo。查看code for the democode for iframe

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-13
      • 2014-07-04
      • 2015-09-24
      • 1970-01-01
      • 2016-06-22
      • 2013-07-23
      • 2011-12-03
      • 1970-01-01
      相关资源
      最近更新 更多