如何调用顶部框架中的javascript函数?
这看起来很简单,但是如何从子框架调用顶级 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码看起来是正确的。如果它说
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 settingonload="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.您是否尝试过这样称呼它:
我假设您在第一帧内有第二帧。
Have you tried calling it like this:
I am assuming you have the 2nd frame inside the 1st frame.
事实上,你就是这样做的。
诀窍是顶级 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 :-)