Jquery在父窗口中从子窗口运行函数
我有一个弹出窗口,我想从父窗口中运行一个函数。
在我的孩子中:
$(document).ready(function(){
parent.$.fn.guestFromPop('Action');
});
在我的父母中:
$(document).ready(function(){
function guestFromPop(data) {
alert(data);
}
});
问题是当 parent.$.fn.guestFromPop('Action');
我从未看到警报时,什么也没有发生。我做错了什么吗?
I have a popup window and I want to run a function from in the parent window.
In the child I have:
$(document).ready(function(){
parent.$.fn.guestFromPop('Action');
});
In the parent I have:
$(document).ready(function(){
function guestFromPop(data) {
alert(data);
}
});
The problem is when parent.$.fn.guestFromPop('Action');
I never see the alert, nothing happens. Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不会声明函数
$.fn.guestFromPop
:...它所做的只是声明
guestFromPop()
,一个仅在该文档中可用的函数。准备好处理程序。您需要声明您想要的函数:
不过,这也不是真正正确的,因为它不会在 jQuery 元素上调用,您可能想要像 : 这样简单的东西,
通过以下方式调用:
This doesn't declare the function
$.fn.guestFromPop
:...all it does is declare
guestFromPop()
, a function only available inside thatdocument.ready
handler. You would need to declare the function you're after instead:Though, this isn't really correct either, since it wont be called on a jQuery element, you may want something as simple as :
called by:
//可在其他父页面或iframe中调用
//
//callable in other parent page or iframe
//
我也有同样的问题。想通了。您的代码的问题是您没有引用正确的窗口来调用该函数。要从控制台到达其父控制台,您需要“opener”,而不是“parent”。
因此,您需要调用以下函数,而不是parent.guestFromPop('Action'):
That 应该引用正确的函数。这对我有用! ;-)
I had the same problem. Figured it out. The problem with your code is that you're not referring to the right window to call the function from. To get from a console to it's parent you'll need 'opener', not 'parent'.
So instead of parent.guestFromPop('Action'), you need to call this:
That should refer to the correct function. It worked for me! ;-)
从 iframe 调用:
call from iframe: