“window.frames[0].variable”在 jquery $(function(){}) 中不起作用?

发布于 2024-10-06 00:10:54 字数 323 浏览 0 评论 0原文

parent.html 中,我设置了一个包含 child.html 的 iframe。

在子框架中,我写了 js:

$(function () { 
    var child = 6;
})

在父框架中,我写了 js:

$(function () {
    alert(window.frames[0].child);
});

但警报结果是“未定义”?

如何使用 jquery 正确引用另一个框架的变量?

In parent.html I set an iframe with child.html in it.

in child frame,I write js:

$(function () { 
    var child = 6;
})

in parent frame,I write js:

$(function () {
    alert(window.frames[0].child);
});

but the alert result is "undefined"?

How can I quote another frame's variable correctly with jquery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2024-10-13 00:10:54

您看到 undefined 是因为您将其声明为局部变量(范围仅限于 document.ready 处理程序),您需要在子框架中使用它全局:

$(function () { 
    window.child = 6;
});

此外,不能保证父框架上的 document.ready 不会在子框架之前执行(坚持 alert() 在每个中查看顺序)...实际上应该是相反的。如果您不立即使用它,这不是问题……如果您需要稍后执行它。

You're seeing undefined because you're delcaring it as a local variable (scoped only to that document.ready handler), you would need this in the child frame for a global:

$(function () { 
    window.child = 6;
});

Also, there's no guarantee that a document.ready on the parent frame won't execute before the child frame (stick an alert() in each to see the order)...in fact it should be the opposite. If you're not using it immediately, it's not an issue...if you are you need to execute it later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文