javascript - 如何在相邻的 iframe 内调用 javascript 函数

标签 javascript jquery iframe

我有一个 html 页面,其中基本上有这样的布局:

<html>
<body>

<!-- iFrame A -->
<iframe></iframe>

<!-- iFrame B -->
<iframe></iframe>
</body>
</html>

在 IFRAME“B”中,我想调用 IFRAME“A”中的 js 函数,这最终将更改 window.location 属性并重定向页面。我也有 jquery 可供使用,但一直无法找到调用相邻帧中某些内容的方法。

有什么建议吗?

最佳答案

假设所有内容都在同一个域中,并且您有两个 ID 为“iframe-a”和“iframe-b”的 iframe,父级中包含 jQuery:

在框架 A 中:

function foo() {
  alert("foo from frame A");
}

来自帧 b:

window.parent.$("#iframe-a")[0].contentWindow.foo();

您应该会看到“foo from frame A”收到警报。

关于javascript - 如何在相邻的 iframe 内调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3693173/

相关文章:

jquery - 为什么路由找不到小数点

javascript - 如何向 rel ="value"添加多个标签/字符串

javascript - javascript 上的基本 if/else 不起作用

javascript - 无法访问 iframe 中加载的自己域上的 httponly 标记的 cookie

internet-explorer - 是否可以在没有内存泄漏的情况下在 IE 中使用 iframe?

javascript - Google Analytics 和 Google 跟踪代码管理器中的产品范围维度

javascript - 使用 JavaScript 更新 anchor

javascript - Ajax发送数据Json类型错误

javascript - 如何确定元素上设置了 CSS 属性?

javascript - 如何防止 iframe 调整父窗口的大小?