如何调用顶部框架中的javascript函数?

发布于 2024-09-06 07:26:28 字数 397 浏览 2 评论 0原文

这看起来很简单,但是如何从子框架调用顶级 html 中定义的 javascript 函数呢?

top html doc
 - 1st level frame
    - 2nd level frame

我的顶级 html 文档有一个名为 testFn() 的函数。在第二级框架中,我有一个带有 onclick="top.testFn();" 的按钮,但这不会调用 testFn()。在 Firebug 中,如果我使用手表执行 top.testFn(); 它会显示 TypeError: testFn() is not a function。

这一切都应该有效吗 - 在这种情况下,这是我的文档的问题,或者是否有其他方法可以调用顶级窗口中的函数?

This seems really simple, but how do I call a javascript function that's defined in the top-level html, from a child frame?

top html doc
 - 1st level frame
    - 2nd level frame

my top html doc has a function called testFn(). In the 2nd level frame I have a button with onclick="top.testFn();" but this doesn't call the testFn(). In Firebug if I use a watch to execute top.testFn(); it says TypeError: testFn() is not a function.

Should this all just work - in which case it's a problem with my documents, or is there some other way to call functions in the top-level window?

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

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

发布评论

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

评论(3

一江春梦 2024-09-13 07:26:28

你的代码看起来是正确的。如果它说 testFn() is not a function 那么也许它不是。您是否尝试过在顶部框架的 中设置 onload="testFn();" 以确保其有效?

可能存在一些 JavaScript 错误,导致创建 testFn 函数的代码无法成功完成。

Your code looks right. If it says testFn() is not a function then perhaps it isn't. Have you tried setting onload="testFn();" in the <BODY> of your top frame to make sure that works?

There could be some javascript error which leads to the code creating your testFn function not being successfully completed.

梦幻的心爱 2024-09-13 07:26:28

您是否尝试过这样称呼它:

onclick="parent.parent.testFn();"

我假设您在第一帧内有第二帧。

Have you tried calling it like this:

onclick="parent.parent.testFn();"

I am assuming you have the 2nd frame inside the 1st frame.

遗失的美好 2024-09-13 07:26:28

事实上,你就是这样做的。

诀窍是顶级 html 页面上的 javascript 中的其他地方没有语法错误:-)

Actually, that is exactly how you do it.

The trick is not having a syntax error elsewhere in the javascript on that top-level html page :-)

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